Getting a specific category of data from firebase

廉价感情. 提交于 2019-12-22 11:21:29

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/51406414/getting-a-specific-category-of-data-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!