Now what I am trying to do is to get all the children having category
value of shop
I have tried this code
Firebase ref = new Firebase("https://top-africa.firebaseio.com/businesses/);
ref.orderByChild("category").equalTo("shop");
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Object ob = dataSnapshot.getValue();
System.out.println("There are " + dataSnapshot.getKey() + " blog posts==" + dataSnapshot.getValue());
}
});
but when I look at the log it printed out all 10 children whereas I suppose I would retrieve only one because there was only one value for category
shop
.
I don't know what I am missing. Could you please help me to solve the issue?
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.
You can use the reference to the new data returned by the push() method to get the value of the child's auto-generated key or set data for the child. Calling getKey() on a push() reference returns the value of the auto-generated key.
public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.
This chat application allows users to store the basic profile and contact list. The user profile would be located on a path such as Users/$uid. User is a node in it and will have a sort of primary key associated with an ID. So, we can access each one uniquely.
your reference is pointing to bussiness, so use query to fetch the data with condition
Query mQuery = ref.orderByChild("category").equalTo("Shop");
mQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Object ob = dataSnapshot.getValue();
System.out.println("There are " + dataSnapshot.getKey() + " blog posts==" + dataSnapshot.getValue());
}
});
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