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

南楼画角 提交于 2020-01-11 14:42:17

问题


This is my firebase realtime database :

users
     company: "Indifair"
     image
     location: "Bangalore"
     name: "Anupam Singh"
     name_title_company_location: "Anupam Singh_Engineer_Indifair_Bangalore"
     number: 956156494
     title: "Engineer"

This is my code :

            DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
            EditText t1 = (EditText) findViewById(R.id.editText10);
            EditText t2 = (EditText) findViewById(R.id.editText11);
            EditText t3 = (EditText) findViewById(R.id.editText13);
            EditText t4 = (EditText) findViewById(R.id.editText14);

            final String s1 = t1.getText().toString();
            final String s2 = t2.getText().toString();
            final String s3 = t3.getText().toString();
            final String s4 = t4.getText().toString();

            mDatabase.child("users").orderByChild("name_title_company_location")
                    .equalTo(s1 + "_" + s2 + "_" + s3 + "_" + s4)
                    .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
            editText11 => Engineer
            editText13 => Indifair
            editText14 => Bangalore

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

What am I doing wrong?


回答1:


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:


-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



来源:https://stackoverflow.com/questions/59608298/firebase-on-android-datasnapshot-getchildrencount-is-returning-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!