【发布时间】:2021-07-14 02:38:50
【问题描述】:
我一直在尝试找到一种方法来通过子节点“投票”进行查询,以按“投票”数对数据进行排序。我很确定,因为我将数据存储在 firebase 中的方式无法查询,但我目前必须以这种方式存储它。
{
"45" : {
"Biological and Ecological" : {
"Votes" : 1
}
},
"567" : {
"Technology" : {
"Votes" : 2
}
},
"620" : {
"Technology" : {
"Votes" : 1
}
},
"702" : {
"Social and Behavioral" : {
"Votes" : 1
}
}
}
我试图通过孩子“投票”来订购有没有办法做到这一点?下面是它当前显示的方法我知道 .OrderbyChild("Votes") 没有正确定义路径,但有没有办法实现这一点?
Query query = FirebaseDatabase.getInstance().getReference("CountLikes/").orderByChild("Votes");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot child: dataSnapshot.getChildren()) {
for(DataSnapshot grandchild: child.getChildren()) {
arrayList.add("Project " + child.getKey() + " " + grandchild.getKey() + " " + (String.valueOf(grandchild.child("Votes").getValue(Long.class))));
adapter.notifyDataSetChanged();
}
}}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
【问题讨论】:
-
嗨! “没有正确定义路径,但有办法实现这一点”。你试过了吗?
标签: java firebase firebase-realtime-database