Java 按时间戳按升序对 Firestore 数据进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50224638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Order Firestore data by TimeStamp in Ascending order
提问by Ebad Ali
I am storing Ideas posted by the application in Firestore. The data is stored in Firestore like this Ideas/{documentID}/IdeaObject. The issue is when I retrieve the data it is not sorted w.r.t time it was posted. The ideas that are retrieved are in according to the id's of their documentID which is automatically create by Firestore. I have used ServerTimestampin my Model Class and also when I retrieve it, I use the orderBy
method with my Firestore reference but still nothing.
我正在 Firestore 中存储应用程序发布的想法。数据像这样Ideas/{documentID}/IdeaObject一样存储在 Firestore 中。问题是当我检索数据时,它没有按发布时间排序。根据 Firestore 自动创建的 documentID 的 ID 检索到的想法。我在我的模型类中使用了ServerTimestamp,并且在检索它时,我将该orderBy
方法与我的 Firestore 引用一起使用,但仍然没有。
Idea.java
想法.java
public class Idea {
@ServerTimestamp
private Date date;
private String title, idea, timeCommitment, ideaStage, postedBy, website, videoPitch;
private int views, favorites;
private ArrayList<String> lookingFor = new ArrayList<>();
private ArrayList<String> tags = new ArrayList<>();
private String userID;
private String timeStamp;
public Idea() {
}
public Idea(String title, String idea, String timeCommitment, String ideaStage, String postedBy, String website, String videoPitch, int views, int favorites, ArrayList<String> lookingFor, ArrayList<String> tags, String userID, String timeStamp) {
this.title = title;
this.idea = idea;
this.timeCommitment = timeCommitment;
this.ideaStage = ideaStage;
this.postedBy = postedBy;
this.website = website;
this.videoPitch = videoPitch;
this.views = views;
this.favorites = favorites;
this.lookingFor = lookingFor;
this.tags = tags;
this.userID = userID;
this.timeStamp = timeStamp;
}
Ideas Posting Method
想法发表方式
private void postIdea() {
final String ideaID = UUID.randomUUID().toString();
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Idea ideas = new Idea(title, idea, timeCommitment, ideaStage, AppValues.fullName, website, videoPitch, 0, 0, lookingFor, tags, AppValues.userId, "" + timestamp.getTime());
firestoreDb.collection("ideas")
.document(ideaID)
.set(ideas)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
postIdeaUser(ideaID);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
hideLoadingDialog();
showToast(e.getMessage());
}
});
}
Retrieving all Ideas by Time
按时间检索所有想法
firestoreDb.collection("ideas")
.orderBy("timeStamp", Query.Direction.ASCENDING)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
ideaArrayList = new ArrayList<>();
ideaArrayList.clear();
for (DocumentSnapshot documentSnapshot : task.getResult()) {
Idea idea = documentSnapshot.toObject(Idea.class);
if (!idea.getUserID().equals(AppValues.userId)) {
ideaArrayList.add(idea);
}
}
callAdapter();
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
progressBar.setVisibility(View.GONE);
swipeRefresh.setEnabled(true);
errorText.setVisibility(View.VISIBLE);
}
}
});
What I want to achieve is retrieve all the ideas and have them ordered ascending by the TimeStamp value.
我想要实现的是检索所有想法并让它们按时间戳值升序排列。
采纳答案by Alex Mamo
You cannot use a String (timeStamp
) when querying your database instead of a Date (date
) and expect to behave as it was a date. So to solve this, please change the following line of code:
timeStamp
查询数据库时不能使用 String ( ) 而不是 Date ( date
) 并期望其行为与日期一样。因此,要解决此问题,请更改以下代码行:
firestoreDb.collection("ideas")
.orderBy("timeStamp", Query.Direction.ASCENDING)
to
到
firestoreDb.collection("ideas")
.orderBy("date", Query.Direction.ASCENDING)
回答by Vince Banzon
In my case, the vanilla version would be,
就我而言,香草版本是,
firestoreDb.collection("ideas")
.orderBy("timestamp", "asc")
firestoreDb.collection("ideas")
.orderBy("timestamp", "desc")
"ideas" is the name of your collection
“ideas”是您收藏的名称
"timestamp" is the key or field or column name to be used for sorting.
“时间戳”是用于排序的键名或字段名或列名。
"asc" or "desc" is the option to be used for the order
"asc" 或 "desc" 是用于订单的选项