如何从android中的firebase获取child值的孩子? [英] How to get child of child value from firebase in android?

查看:19
本文介绍了如何从android中的firebase获取child值的孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取 ZNAME 值?最初我需要比较 key(Ex::Here ZONE_1) 然后需要获取 ZNAME.提前致谢...

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

推荐答案

要访问数据库中的值,请为该位置创建一个 DatabaseReference.以下是对数据库中位置的三个引用:

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");

在这个片段中:

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

请参阅 有关获取数据库参考的 Firebase 文档 了解更多信息.

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

您可以为每个引用附加一个侦听器,以获取该位置的值.例如,要获取/ZONES/ZONE_1/ZNAME 的值:

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

有关此类读取操作的更多信息,请参阅 Firebase关于读取值的文档.

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

如果您改为监听 /ZONES/ZONE_1,您将获得整个节点的 DataSnapshot 及其所有属性.然后使用 DataSnapshot.child() 从中获取 ZNAME:

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

再上一层,您可以收听 /ZONES,这将为您提供所有区域的快照.由于这会处理多个子项,因此您需要使用 DataSnapshot.getChildren() 遍历它们:

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

有关这方面的更多信息,请参阅 有关侦听的 Firebase 文档数据列表.

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

最后,您可能想要查询以查找特定区域,例如使用 "ZCODE": "ECOR":

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

要了解更多相关信息,请阅读 有关排序的 Firebase 文档和过滤数据.

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

这篇关于如何从android中的firebase获取child值的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆