Firebase android订购

时间:2017-02-20 10:14:36

标签: android firebase firebase-realtime-database

我无法在日期字段(时间戳)上使用orderByChild订购结果

这里的数据结构:

enter image description here

代码:

  DatabaseReference mMessagesRef = FirebaseDatabase.getInstance().getReference().child("messages")
                .child(mConversationId).getRef()
                .orderByChild("date").getRef();
        mMessagesRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for(DataSnapshot data: dataSnapshot.getChildren()){

查询返回了值,但没有按日期排序,它按键返回..

1 个答案:

答案 0 :(得分:4)

Try this:
DatabaseReference mMessagesRef=FirebaseDatabase.getInstance().getReference();
Query queryRef = mMessagesRef.child("messages").orderByChild("date");
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for(DataSnapshot data: dataSnapshot.getChildren()){