首页 > 解决方案 > How can I orderByChild on Android Firebase chat based application?

问题描述

I'm trying to build a user to user chat application, but I'm not able to order the chat list by last message sent.

My Firebase (real time) structure is like this:

Structure

I'm trying to retrieve the data like this, but i'm not able to order by "timestamp" in the inner message.

final String uID = firebaseAuth.getCurrentUser().getUid();
Query ref = FirebaseDatabase.getInstance().getReference("Messages");
ref.orderByChild("timestamp").startAt(uID).addValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

Did anyone knows how can I order the list of users by the timestamp of the last message?

标签: androidlistfirebasefirebase-realtime-databasechat

解决方案


您需要将查询更改为以下内容:

final String uID = firebaseAuth.getCurrentUser().getUid();
Query ref = FirebaseDatabase.getInstance().getReference("Messages");
ref.child(uID_uID).orderByChild("timestamp").addValueEventListener(new ValueEventListener() {

您需要访问 node 下的直接节点Messages,即带有下划线的节点。然后你可以添加orderByChild("timestamp").equalTo("157644739279")


推荐阅读