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

我的未来我决定 提交于 2019-12-27 13:56:49

问题


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...


回答1:


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.



来源:https://stackoverflow.com/questions/43293935/how-to-get-child-of-child-value-from-firebase-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!