Java 如何安排从firebase firestore获取的数据顺序
我已经将用户数据发布到Firebase Firestore,并且我已经成功地检索到了数据,现在我想让它成为帖子墙中的最后一篇文章。Java 如何安排从firebase firestore获取的数据顺序,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我已经将用户数据发布到Firebase Firestore,并且我已经成功地检索到了数据,现在我想让它成为帖子墙中的最后一篇文章。 在此处输入代码 //Snapshot listener help us to retrive the data in real time firebaseFirestore.collection("user_post").addSnapshotListener(new EventListener<QuerySnapshot>() {
在此处输入代码
//Snapshot listener help us to retrive the data in real time
firebaseFirestore.collection("user_post").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
//check for any changes in document
for (DocumentChange doc: documentSnapshots.getDocumentChanges()){
//check if document get added
if (doc.getType() == DocumentChange.Type.ADDED){
Wall_post wall_post = doc.getDocument().toObject(Wall_post.class);
wallPosts_list.add(wall_post);
wallpostRecyclerAdapter.notifyDataSetChanged();
}
}
}
});
//快照侦听器帮助我们实时检索数据
firebaseFirestore.collection(“user_post”).addSnapshotListener(新的EventListener(){
@凌驾
public void OneEvent(QuerySnapshot文档快照,FireBaseFireStore异常e){
//检查文档中是否有任何更改
对于(DocumentChange文档:documentSnapshots.getDocumentChanges()){
//检查是否添加了文档
if(doc.getType()==DocumentChange.Type.ADDED){
Wall\u post Wall\u post=doc.getDocument().toObject(Wall\u post.class);
墙柱列表。添加(墙柱);
wallpostRecyclerAdapter.notifyDataSetChanged();
}
}
}
});
如果您有一个发布的时间戳,那么您可以在代码中使用它。将时间戳更改为您自己的变量
firebaseFirestore = FirebaseFirestore.getInstance();
Query postOrder = firebaseFirestore.collection("user_post").orderBy("time_stamp",Query.Direction.DESCENDING);
//Snapshot listener help us to retrive the data in real time
postOrder.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
//check for any changes in document
for (DocumentChange doc: documentSnapshots.getDocumentChanges()){
//check if document get added
if (doc.getType() == DocumentChange.Type.ADDED){
Wall_post wall_post = doc.getDocument().toObject(Wall_post.class);
wallPosts_list.add(wall_post);
wallpostRecyclerAdapter.notifyDataSetChanged();
}
}
}
});
firebaseFirestore=firebaseFirestore.getInstance();
Query postOrder=firebaseFirestore.collection(“user\u post”).orderBy(“time\u stamp”,Query.Direction.DESCENDING);
//快照侦听器帮助我们实时检索数据
addSnapshotListener(新的EventListener(){
@凌驾
public void OneEvent(QuerySnapshot文档快照,FireBaseFireStore异常e){
//检查文档中是否有任何更改
对于(DocumentChange文档:documentSnapshots.getDocumentChanges()){
//检查是否添加了文档
if(doc.getType()==DocumentChange.Type.ADDED){
Wall\u post Wall\u post=doc.getDocument().toObject(Wall\u post.class);
墙柱列表。添加(墙柱);
wallpostRecyclerAdapter.notifyDataSetChanged();
}
}
}
});
如果你再清除我一点,这将非常有帮助。我正在编辑代码,请查看并让我知道如果此工作转到此链接,您将找到有关查询的更多信息