[英]How do I retrieve data from multiple nodes in Firebase? Android
要訪問所有用戶的問題,您需要執行以下操作:
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();
}
}
我強烈建議您閱讀有關結構化數據的 Firebase 文檔,因為您在此處嵌套不同類型的數據,這與避免嵌套數據的建議背道而馳。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.