Darshan Gowda
Darshan Gowda

Reputation: 5226

How to get all children data Inside a nested key Database in Firebase Realtime Database Android?

I have a database structure as below. enter image description here

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?

Upvotes: 0

Views: 62

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 1

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!
        }
    }
});

Upvotes: 1

Related Questions