如何在firebase数据库中获取所有孩子的数据? [英] How to get all child's data in firebase database?

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

问题描述

我有这个 firebase 数据库

I have this firebase database

我需要获取用户的所有电话号码,我应该使用哪个监听器来获取所有孩子?

and i need to get all phone numbers of users , which listener shall i use to get all childes?

每个用户都被添加为一个对象,用户 ID 作为该对象的名称,我需要在不知道用户 ID 的情况下检索这个对象

Every user is added as an object with user-ID as a name of that object, I need to retrieve this objects without knowing that user-ID

我搜索了文档,它与 DataSnapshot 相关,但我无法在没有侦听器的情况下获得 DataSnapshot!寻找 DataSnapshout 是正确的还是我应该使用其他东西

I searched the documentation , it's related to DataSnapshot but i couldn't get a DataSnapshot without a listener ! is it correct to seek a DataSnapshout or shall i use something else

推荐答案

首先检索您的用户数据快照.

First retrieve your users datasnapshot.

//Get datasnapshot at your "users" root node
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
    ref.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //Get map of users in datasnapshot
                    collectPhoneNumbers((Map<String,Object>) dataSnapshot.getValue());
                }

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

然后遍历用户,访问他们的地图并收集电话字段.

Then loop through users, accessing their map and collecting the phone field.

private void collectPhoneNumbers(Map<String,Object> users) {

    ArrayList<Long> phoneNumbers = new ArrayList<>();

    //iterate through each user, ignoring their UID
    for (Map.Entry<String, Object> entry : users.entrySet()){

        //Get user map
        Map singleUser = (Map) entry.getValue();
        //Get phone field and append to list
        phoneNumbers.add((Long) singleUser.get("phone"));
    }

    System.out.println(phoneNumbers.toString());
}

这个监听器只会在显式调用时检索数据快照.除了用户节点之外,还可以考虑在allPhoneNumbers"节点下存储号码列表.在收集所有数字时,这将使您的数据快照更轻且更易于处理.如果您说有数百个用户,那么用户"数据快照会太大,而allPhoneNumbers"列表会更有效率.

This listener will only retrieve the datasnapshot when explicitly called. Consider storing a list of numbers under an "allPhoneNumbers" node in addition to your users node. This will make your datasnapshots lighter and easier to process when collecting all numbers. If you have say hundreds of users, the "users" datasnapshot will be way too big and the "allPhoneNumbers" list will be far more efficient.

以上代码已在您的示例数据库上进行了测试并且可以正常工作.但是,您的电话字段可能需要转换为 String 或 int,具体取决于您用户的电话实例字段的类型.

The above code was tested on your sample database and does work. However, your phone field may need to be casted to String or int depending on your user's phone instance field's type.

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

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