How to get child of child value from firebase in android?

To access a value in your database, you create a DatabaseReference for that location. Here are three references to locations in your database:

DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference("ZONES");
DatabaseReference zone1Ref = zonesRef.child("ZONE_1");
DatabaseReference zone1NameRef = zone1Ref.child("ZNAME");

In this snippet:

  • zonesRef points to /ZONES
  • zone1Ref points to /ZONES/ZONE_1
  • zone1NameRef points to /ZONES/ZONE_1/ZNAME

See the Firebase documentation on getting a database reference for more information.

You can attach a listener to each of the references, to get the value at that location. For example, to get the value of the /ZONES/ZONE_1/ZNAME:

zone1NameRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.i(TAG, dataSnapshot.getValue(String.class));
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
});

For more on this type of read operation, see the Firebase documentation on reading values.

If you instead listen on /ZONES/ZONE_1, you will get a DataSnapshot of the entire node with all its properties. You then use DataSnapshot.child() to get the ZNAME from it:

zone1Ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.i(TAG, dataSnapshot.child("ZNAME").getValue(String.class));
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
});

One more level up, you can listen on /ZONES, which will get you a snapshot with all the zones. Since this handles multiple children, you will need to loop through them with DataSnapshot.getChildren():

zonesRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot zoneSnapshot: dataSnapshot.getChildren()) {
            Log.i(TAG, zoneSnapshot.child("ZNAME").getValue(String.class));
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
});

For more on this, see the Firebase documentation on listening for lists of data.

Finally, you might want to query to find a specific zone, for example to find the zone with "ZCODE": "ECOR":

Query zonesQuery = zonesRef.orderByChild("ZCODE").equalTo("ECOR");
zonesQuery.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot zoneSnapshot: dataSnapshot.getChildren()) {
            Log.i(TAG, zoneSnapshot.child("ZNAME").getValue(String.class));
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
});

To learn more about this, read the Firebase documentation on sorting and filtering data.


EASIER METHOD!!

If you want to access the values "Z_Name" of all the child nodes of "Zones" then you can do so using the following code.

    final DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Zones");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){

                    String z_name=snapshot.child("Z_name").getValue().toString();
    
            }

        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });

` the required value is stored in(this line is already present in above code) :

String z_name=snapshot.child("Z_name").getValue().toString();

this variable "z_name" will have the value of Zone_1 in the first iteration of for loop and the value of Zone_2 in the second iteration of for loop respectively.

Extra: If there had been nodes further we could have used datasnapshot.getChildren() again to access it's child nodes