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.