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??
Query countQuery = countRef. child("ques"). child("Technology"); countQuery. addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { questionCount = (int) dataSnapshot.
Query the first level and then in your SnapShot, use : getChildrenCount() . Check this link. And here. Show activity on this post.
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.
Use snapshot. exists() to check if the referenced database entry contains a child , irrespective of the value of the child. Highly active question.
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) {
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With