android - 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:
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?
解决方案
您需要将查询更改为以下内容:
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")
推荐阅读
- gatsby - Gatsby JSS 在页面加载时出现“闪烁”
- php - 左连接后无法在 for-each 循环中显示列
- c - 调用 size_t *size as *z 时读取访问冲突
- c# - C# DBNull.Value 插入 0
- angular - Kendo Grid for Angular - 无法让行选择、分页和排序协同工作
- java - 如何从 START_OBJECT 令牌中反序列化 java.lang.String 的实例
- javascript - 当缩放级别(devicePixelRatio)大于 1 时,画布渲染不起作用
- r - R:读取数据框中的字符串并记录特定字母的位置
- ios - 在iOS中更改轴时如何更改堆栈视图中元素的顺序?
- java - 在 Spark Cassandra 连接器中映射 UUID