我将应用程序发布的想法存储在 Firestore 中.数据像 Ideas/{documentID}/IdeaObject 一样存储在 Firestore 中.问题是当我检索数据时,它没有按发布时间排序.检索到的想法是根据 Firestore 自动创建的 documentID 的 id.我在我的模型类中使用了 ServerTimestamp,当我检索它时,我将 orderBy
方法与我的 Firestore 引用一起使用,但仍然没有.
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 ServerTimestamp in my Model Class and also when I retrieve it, I use the orderBy
method with my Firestore reference but still nothing.
Idea.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;
}
创意发布方式
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());
}
});
}
按时间检索所有创意
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);
}
}
});
我想要实现的是检索所有想法并让它们按 TimeStamp 值升序排列.
What I want to achieve is retrieve all the ideas and have them ordered ascending by the TimeStamp value.
查询数据库时不能使用字符串(timeStamp
)而不是日期(date
)并期望表现得像约会一样.所以要解决这个问题,请更改以下代码行:
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:
firestoreDb.collection("ideas")
.orderBy("timeStamp", Query.Direction.ASCENDING)
到
firestoreDb.collection("ideas")
.orderBy("date", Query.Direction.ASCENDING)
为了使它工作,这种查询需要一个索引.要创建一个,请查看我在以下帖子中的回答:
To make it work, this kind of query requires an index. To create one, please check my answer from the following post:
这篇关于按 TimeStamp 升序排列 Firestore 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!