Firebase在Android上:DataSnapshot.getChildrenCount()返回0

huangapple go评论69阅读模式
英文:

Firebase on Android : DataSnapshot.getChildrenCount() is returning 0

问题

这是我的Firebase实时数据库:

Firebase在Android上:DataSnapshot.getChildrenCount()返回0

这是我的代码:

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字段中输入这些值:

editText10 => Anupam Singh

dataSnapshot.getChildrenCount() 应该为 1,但它却为 0。

我做错了什么?

英文:

This is my firebase realtime database :

Firebase在Android上:DataSnapshot.getChildrenCount()返回0

This is my code :

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

I am entering these values in EditText fields :

            editText10 => Anupam Singh

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

What am I doing wrong?

答案1

得分: 1

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

users
    name_title_company_location

因此,属性 name_title_company_location 是节点 users 的直接子级。当你使用 orderByChild 时,你并没有访问该节点。

你需要在数据库中添加一个 id

users
    id
     name_title_company_location

然后你的代码将会工作。


或者你可以将代码更改如下,向上移动一级:

mDatabase.orderByChild("name_title_company_location").equalTo(...)
英文:

In your database you have the following:

users
    name_title_company_location

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

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

答案2

得分: 0

-the issue is in orderByChild.
问题出在 orderByChild。

-you have pass query and ask for single value event listener.
你已经传递了查询并请求单个值事件监听器。

do as below :
按照以下方式操作:

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users");

Query query = mDatabase.child("name_title_company_location")
.equalTo(s1 + "" + s2 + "" + s3 + "_" + s4);

query.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView tv2 = (TextView) findViewById(R.id.textView5);
tv2.setText(Long.toString(dataSnapshot.getChildrenCount()));
}
}

-it will return the data
它会返回数据。

英文:

-the issue is in orderByChild.
-you have pass query and ask for single value event listener.

do as below :

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users");

     Query query =  mDatabase.("name_title_company_location")
                .equalTo(s1 + "_" + s2 + "_" + s3 + "_" + s4);

                query.addSingleValueEventListener(new ValueEventListener(){
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                                TextView tv2 = (TextView) findViewById(R.id.textView5);
                                tv2.setText(Long.toString(dataSnapshot.getChildrenCount()));
                    }
                 }

-it will return the data

huangapple
  • 本文由 发表于 2020年1月6日 15:37:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定