Issue
I have a database structure as below.
I am able to retrieve the children's data when I give reference to date but I want to retrieve data without referencing to date but the previous nested key.
That is, when I write the following, I am able to get the data:
databaseReference = FirebaseDatabase.getInstance().getReference("LDC Wise").child("Agra").child("25-10-2022");
But when I write the following, I am getting null data:
databaseReference = FirebaseDatabase.getInstance().getReference("LDC Wise").child("Agra")
How do I go about fetching all the data in the next sublevel of the database without referencing to date key explicitly?
Solution
How do I go about fetching all the data in the next sublevel of the database without referencing to date key explicitly?
This can be solved by looping through the results twice:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference agraRef = db.child("LDC Wise").child("Agra");
agraRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot snapshot : task.getResult().getChildren()) {
for (DataSnapshot innerSnapshot : snapshot.getChildren()) {
//Get the data out of the innerSnapshot object.
}
}
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});
Answered By - Alex Mamo
Answer Checked By - Timothy Miller (JavaFixing Admin)