Firebase retrieves data from a child by key

I have a database structure as shown in the screenshot below

Now I'm trying to do all the children with a categoryvalueshop

I 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 magazine, it prints all 10 children, whereas I believe that I would get only one, because there category shopwas only one value.

I don’t know what I am missing. Could you help me solve the problem?

+4
source share
1 answer

your link points to business, so use a query to retrieve data with a 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());
            }
 });
+4
source

Source: https://habr.com/ru/post/1655182/


All Articles