从Firebase获取特定类别的数据 [英] Getting a specific category of data from firebase

查看:70
本文介绍了从Firebase获取特定类别的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对Firebase实时数据库中的数据进行分类,然后按类别选择它们以显示在我的android视图的不同部分中,但我不知道从哪里开始.我是Firebase的新手,我只知道如何提取数据,推送到Firebase数据库并显示获取的所有数据.

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

推荐答案

如果您有此数据库:

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.

也请在这里检查:

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

这篇关于从Firebase获取特定类别的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆