我正在开发Android应用,允许用户从多个Firebase数据库中读取数据。
由于某些原因,数据未按日期排序并按随机顺序排列。如何按日期对评论进行排序并在列表底部显示最新评论?
ArrayList<Comment> comments = new ArrayList<Comment>();
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Comment comment = dataSnapshot.getValue(Comment.class);
comments.add(comment):
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Comment newComment = dataSnapshot.getValue(Comment.class);
String commentKey = dataSnapshot.getKey();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
String commentKey = dataSnapshot.getKey();
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Comment movedComment = dataSnapshot.getValue(Comment.class);
String commentKey = dataSnapshot.getKey();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
List<String> dbUris = getFirebaseUriList();
for(String dburi: dbUris){
Query query = FirebaseDatabase.getInstance(dburi).getReference().child("comments").limitToLast(15);
query.addChildEventListener(listener);
}
在多个查询中使用相同的侦听器是个好主意吗?谢谢。
答案 0 :(得分:1)
改变这个:
query.addChildEventListener(listener);
进入这个:
query.orderByChild("date").addChildEventListener(listener);
能够按date
更多信息: https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query