Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android firebase - get childrens count

Here is my Firebase data

{
"ques": {
"Chemistry": {},
"Countries": {},
"Film": {},
"Geography": {},
"General Knowledge": {},
"Information Technology": {},
"Physics": {},
"Sports": {},
"Technology": {
"-Ki0y2ojhgAkDKVFostV": {},
"-Ki0ySAkiILNo2WSM7EO": {},
"-Ki1-8R-5gfHJGl9bDYC": {},
"-Ki1-fP2HlW0ejzVOGnD": {},
"-Ki1-vihF3xgP81UPT5t": {},
"-Ki10DSBPWIK0SAv5v8V": {},
"-Ki11-pLXVWZlv0XbKpp": {},
"-Ki11al-z6_MDBdpLGh7": {},
"-Ki11pxZvjvIl68UhVRc": {},
"-Ki124jOsYpy9bWpc7Yo": {},
"-Ki12oDiad-7X6UBTtBv": {},
"-Ki133GHJeys-J0vtdI9": {},
"-Ki13Iq62Ly_tx4tcxKa": {},
"-Ki13e4pp1Xnq7tnYa4N": {},
"-Ki142gU6ZHRkE34r0u3": {},
"-Ki14GswYf_lEji5a2PX": {},
"-Ki14WZpH5w5jnB4mZF1": {},
"-Ki14tX__g6VdmHJHi-g": {},
"-Ki15JghcU73RtW5zacV": {},
"-Ki15c91cOoRoR7h5aTL": {}
}
}
}

I want to count the number of keys available under Technology When I use the below code , gets count = 7 , but there is 20 keys available under Technology

Query countQuery = countRef.child("ques").child("Technology");
countQuery.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {


                questionCount = (int) dataSnapshot.getChildrenCount();
                tQuestionNoShow.setText(""+questionCount);

            }

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

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

So.. How to implement it??

like image 816
Ashique bzq Avatar asked Apr 25 '17 09:04

Ashique bzq


People also ask

How to get children count in Firebase Android?

Query countQuery = countRef. child("ques"). child("Technology"); countQuery. addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { questionCount = (int) dataSnapshot.

How to get the child count in Firebase database?

Query the first level and then in your SnapShot, use : getChildrenCount() . Check this link. And here. Show activity on this post.

How to fetch data Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

How do I check my child's firebase?

Use snapshot. exists() to check if the referenced database entry contains a child , irrespective of the value of the child. Highly active question.


1 Answers

  Firebase.setAndroidContext(this);
  DatabaseReference fbDb = null;
  if (fbDb == null) {
      fbDb = FirebaseDatabase.getInstance().getReference();
  }


  fbDb.child("ques/Technology")
      .addListenerForSingleValueEvent(new ValueEventListener() {
           @Override
           public void onDataChange(DataSnapshot dataSnapshot) {
               // get total available quest
                int size = (int) dataSnapshot.getChildrenCount();
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
like image 71
Rjz Satvara Avatar answered Sep 17 '22 23:09

Rjz Satvara