我尝试从每个用户的questions节点中检索问题,如下图所示。我想不出一个到达节点的方法。如何从不同用户的每个问题节点检索数据?谁来帮帮忙。
发布于 2020-06-16 13:53:23
要访问所有用户的问题,您可以执行以下操作:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
DataSnapshot questionsSnapshot = userSnapshot.child("questions");
Log.i(TAG, "User "+userSnapshot.getKey()+" has "+questionsSnapshot.numChildren()+" questions");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
我强烈建议您阅读structuring data上的Firebase文档,因为您在这里嵌套了不同类型的数据,这与对avoid nesting data的建议背道而驰。
https://stackoverflow.com/questions/62404375
复制相似问题