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??
Solution 1:
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) {
}
});