简体   繁体   English

Firebase按键从子项检索数据

[英]Firebase retrieve Data from child by key

我在快照中有如下所示的数据库结构

Now what I am trying to do is to get all the children having category value of shop 现在我要做的是让所有孩子都有shop category价值

I have tried this code 我试过这段代码

Firebase ref = new Firebase("https://top-africa.firebaseio.com/businesses/);
ref.orderByChild("category").equalTo("shop");
        ref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot,  String s) {
                Object ob = dataSnapshot.getValue();
                System.out.println("There are " + dataSnapshot.getKey() + " blog posts==" + dataSnapshot.getValue());
            }
 });

but when I look at the log it printed out all 10 children whereas I suppose I would retrieve only one because there was only one value for category shop . 但是当我查看日志时,它打印出所有10个孩子,而我想我只会检索一个,因为category shop只有一个值。

I don't know what I am missing. 我不知道我错过了什么。 Could you please help me to solve the issue? 你能帮我解决一下这个问题吗?

your reference is pointing to bussiness, so use query to fetch the data with condition 您的引用指向商务,因此使用查询来获取条件数据

Query mQuery = ref.orderByChild("category").equalTo("Shop");
mQuery.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot,  String s) {
                Object ob = dataSnapshot.getValue();
                System.out.println("There are " + dataSnapshot.getKey() + " blog posts==" + dataSnapshot.getValue());
            }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM