如何安排我从firebase firestore获得的数据的顺序

时间:2018-06-18 13:49:07

标签: java android firebase google-cloud-firestore

我已将用户数据发布到Firebase Firestore,并且我已成功检索数据,现在我想按顺序将其发布到帖子墙中的第一篇帖子中。 enter code here

 //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();
                    }
                }
            }
        });

1 个答案:

答案 0 :(得分:0)

如果您有发布的时间戳,那么您可以在代码中使用它。将time_stamp更改为您拥有的变量。

Query postOrder = firebaseFirestore.collection("user_post").orderBy("time_stamp",Query.Direction.DESCENDING);
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();
            }
        }     
    }
});