Reputation: 143
I am new to firebase and would like to get all stores records that contain a specific key in the "category" key, How can I retrieve a result from the data above WHERE CATEGORY = CATEGORY_09
My database:
{
"Shops" : {
"shop_01" : {
"adress" : "Calle Belisario Domínguez 3,",
"category" : "category_08",
"description" : "One Description",
"email" : "[email protected]",
"facebook" : "www.facebook.com/profile",
"horary" : "8 am - 10pm",
"image" : "https://firebasestorage.googleapis.com/v0/b/...",
"latitude" : 19.561588,
"longitude" : -97.240889,
"name" : "Grupo Culinario Luiggi's",
"phone" : 28128253532
}
},"shop_02" : {
"adress" : "Rafael Murillo Vidal",
"category" : "category_09",
"description" : "One description",
"email" : "[email protected]",
"facebook" : "www.facebook.com/profile",
"horary" : "8 am - 10pm",
"image" : "https://firebasestorage.googleapis.com/v0/b/...",
"latitude" : 19.561588,
"longitude" : -97.240889,
"name" : "Grupo Culinario Luiggi's",
"phone" : 232321532
}
}
I hope to get your help thank you very much
Upvotes: 0
Views: 73
Reputation: 1235
DatabaseReference database = FirebaseDatabase.getInstance().getReference("shops");
Query category = database.orderByChild("category").equalTo(CATEGORY_09);
category.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// get the data here of CATEGORY_09
}
...
}
I assumed Shops as a reference for your shops table
Upvotes: 1
Reputation: 149
Checking the category every time, can solve the problem.
public class Main_Activity extends AppCompatActivity {
private DatabaseReference mList;
private FirebaseDatabase mFirebaseInstance;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseInstance = FirebaseDatabase.getInstance();
mList = mFirebaseInstance.getReference("Shops");
mList.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
if(child.child("category").getValue().toString().equals("category_09"){
String adress = child.child("adress").getValue().toString();
String facebook = child.child("facebook").getValue().toString();
String latitude = child.child("latitude").getValue().toString();
String name = child.child("name").getValue().toString();
String longitude = child.child("longitude").getValue().toString();
//then just add them to a list or db
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Upvotes: 0