Otema :
I would like to categorize data in a Firebase realtime database and then select them by category to be displayed in different sections of my android views but I don't know where to start. I am new to Firebase all I know is how to pull data, push to Firebase db and display all the data fetched.
My question is: is this possible and if yes how can I go about it? Thanks
Peter Haddad :
If you have this database:
Animals
randomId
name: dog
category: mammal
randomId
name: snake
category: reptile
randomId
name: elephants
category: mammal
Then you can query by category:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference("Animals");
Query query=ref.orderByChild("category").equalTo("mammal");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String name=datas.child("name").getValue().toString();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
This way you will retrieve all the names where category is equal to mammal.
Check here also:
https://firebase.google.com/docs/database/android/lists-of-data