To access the questions of all users you'd do something like this:
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();
}
}
I highly recommend reading the Firebase documentation on structuring data as you are nesting data of different types here, which goes against the recommendation to avoid nesting data .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.