简体   繁体   中英

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

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 .

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());
            }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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