From the firebase example, I need to find out the if there are any dinosaurs of age=25, which can be done as below. But say if there are no dinosaurs of that age, how can I find out that the query is finished and there are 0 dinosaurs of age=25. because my Android UI depends on this, to proceed to the next step.
Firebase ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
Query queryRef = ref.orderByChild("height").equalTo(25);
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChild) {
System.out.println(snapshot.getKey());
}
// ....
});
EDIT Some solutions provided are suggesting to use ValueEventListener. But the problem is even if you use valueEventListener, in the above case, it still does not work,as there are 0 rows. The onDataChange does not fire.
Firebase ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
Query queryRef = ref.orderByChild("height").equalTo(25);
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChanged(DataSnapshot snapshot) {
System.out.println(snapshot.getKey());
}
// ....
});
ANSWER
@Override
public void onDataChange(DataSnapshot snapshot) {
//DinosaurFacts facts = snapshot.getValue(DinosaurFacts.class);
//Log.d("hz-dino", facts.toString());
if(snapshot.getValue() != null)
{
Log.d("hz-dino", snapshot.getKey());
Log.d("hz-dino", String.valueOf(snapshot.getValue()));
}
else
{
Log.d("hz-dino", "there are exactly 0 rows!");
}
}