如何获取Firebase Android中具有特定值的孩子数量? [英] How to get the number of children with specific value in firebase android?

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

问题描述

我想获取状态为不正常"的所有化合物的值,您可以在我上传的图片中看到该值.我已经尝试过了,但是我没有得到值.请帮助我为我要实现的问题创建正确的查询.

I want to get the value of all compounds with the status NOT OK, which you can see in the picture i uploaded. I have tried this, but I failed to get the values. Please help me on creating the correct query for what I want to achieve.

mUserCompounds.orderByChild("status").equalTo("NOT OK").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            long compounds_solved = dataSnapshot.getChildrenCount();
            String output = String.valueOf(compounds_solved);
            Log.d("OUTPUT", "Total not ok " + output);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            long compounds_solved = dataSnapshot.getChildrenCount();
            String output = String.valueOf(compounds_solved);
            Log.d("OUTPUT", "Total not ok " + output);
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

我想为compounds_solved获取的结果是4.但是我根据我的代码得到的是这个.

What i wanted to obtain for the compounds_solved is 4. But what I get based on my code is this.

推荐答案

ChildEventListener将为您提供发生事件的子项的数据.因此,当您获得快照中的子代数量时,实际上就是在获得属性数量的计数.图片,名称和状态(3).

ChildEventListener will give you the data of the child on which an event occured. So when you get the amount of children on the snapshot you're actually getting the count of the amount of properties. Image, name and status (3).

改为使用ValueEventListener.

Use ValueEventListener instead.

mUserCompounds.orderByChild("status").equalTo("NOT OK").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        System.out.println(dataSnapshot.getChildrenCount());                        }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

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

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