Java – Get specific categories of data from firebase

Get specific categories of data from firebase… here is a solution to the problem.

Get specific categories of data from firebase

I

want to categorize the data in the Firebase live database and then select them by category to display in different parts of my android View, but I don’t know where to start. I’m new to Firebase, I just know how to extract data, push it to the Firebase database, and display all the acquired data.

My question is: is this possible? If so, what should I do?
Thanks

Solution

If you have this database:

Animals
   randomId
       name: dog
       category: mammal
   randomId
       name: snake
       category: reptile
   randomId
       name: elephants
       category: mammal

You can then query by class:

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 will retrieve all names with categories equal to mammals.

Check here too:

https://firebase.google.com/docs/database/android/lists-of-data

Related Problems and Solutions