Android上的Firebase:DataSnapshot.getChildrenCount()返回0 [英] Firebase on Android : DataSnapshot.getChildrenCount() is returning 0

查看:64
本文介绍了Android上的Firebase:DataSnapshot.getChildrenCount()返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的firebase实时数据库:

This is my firebase realtime database :

这是我的代码:

            DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
            EditText t1 = (EditText) findViewById(R.id.editText10);

            final String s1 = t1.getText().toString();

            mDatabase.child("users").orderByChild("name")
                    .equalTo(s1)
                    .addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                                    TextView tv2 = (TextView) findViewById(R.id.textView5);
                                    tv2.setText(Long.toString(dataSnapshot.getChildrenCount()));
                        }
                     }

我正在EditText字段中输入这些值:

I am entering these values in EditText fields :

            editText10 => Anupam Singh

dataSnapshot.getChildrenCount()应该为1,但即将为0.

dataSnapshot.getChildrenCount() should be 1 but it is coming to be 0.

我在做什么错了?

推荐答案

在您的数据库中,您具有以下内容:

In your database you have the following:

users
    name_title_company_location

因此,属性name_title_company_location是节点users下的直接子代.使用orderByChild时,您没有访问该节点.

Therefore the attribute name_title_company_location is a direct child under nodeusers. When you are using orderByChild, you are not accessing that node.

您需要做的是在数据库中添加id:

What you need to do is add an id in the database:

users
    id
     name_title_company_location

然后您的代码即可使用.

Then your code will work.

或者您可以将代码更改为以下代码,向上一级:

Or you can change the code to the following, go one level up:

mDatabase.orderByChild("name_title_company_location").equalTo(...)

这篇关于Android上的Firebase:DataSnapshot.getChildrenCount()返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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