我希望聊天列表按降序排列。 ex->向我发送最后一条消息的人应该出现在我的顶部。 我使用了orderByChild(),但是没有用。请帮助我。如果这不起作用,则可以通过对Hashmap的ArrayList进行排序来帮助我获取此类数据。请帮助我。
Query query = mReference.child(AppSettings.getString(AppSettings.userId)).child(postSnapshot.getKey()).orderByChild("time");
query.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//for comparing childCount and innerChildCount to get last chat message data;
long innerChildCount=1;
String key=dataSnapshot.getKey();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
if (finalChildCount == innerChildCount){
if (key.equals(String.valueOf(postSnapshot.child("senderId").getValue()))){
HashMap hashMap=new HashMap();
hashMap.put("userId",""+postSnapshot.child("senderId").getValue());
hashMap.put("name",""+postSnapshot.child("senderName").getValue());
hashMap.put("profile",""+postSnapshot.child("senderProfileUrl").getValue());
hashMap.put("message",""+postSnapshot.child("message").getValue());
arrayList.add(hashMap);
}
else if (key.equals(String.valueOf(postSnapshot.child("receiverId").getValue()))){
HashMap hashMap=new HashMap();
hashMap.put("userId",""+postSnapshot.child("receiverId").getValue());
hashMap.put("name",""+postSnapshot.child("receiverName").getValue());
hashMap.put("profile",""+postSnapshot.child("receiverProfileUrl").getValue());
hashMap.put("message",""+postSnapshot.child("message").getValue());
arrayList.add(hashMap);
}
}
innerChildCount=innerChildCount+1;
// adapter.notifyDataSetChanged();
}
adapter.notifyDataSetChanged();
endLoopCount=endLoopCount+1;
}
这是我的数据库结构: 我要在“时间”键中保存时间戳