Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How to get ZNAME value? Initially i need to compare key(Ex::Here ZONE_1) and then ZNAME need to be get. Thanks in advance...

like image 912
Surekha Avatar asked Apr 08 '17 12:04

Surekha


People also ask

How do I get particular data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

How do I get data from Firebase in react?

Step 1: Create a new React application. We use create-react-app to create our application. Step 2: Install the firebase package in the project using npm. Step 3: Create a new project from the Firebase dashboard by filling in the necessary details and check the format of the data that is stored in Firestore.

What is DataSnapshot in Firebase?

A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot. DataSnapshots are passed to the methods in listeners that you attach with addValueEventListener , addChildEventListener , or addListenerForSingleValueEvent .


2 Answers

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.

like image 62
Frank van Puffelen Avatar answered Oct 04 '22 01:10

Frank van Puffelen


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

like image 38
Manish Patel Avatar answered Oct 04 '22 01:10

Manish Patel