Skip to content Skip to sidebar Skip to footer

How To Retrieve Data From Firebase Realtime Database In Android?

I am new here. I am not able to retrieve data from Firebase database. I want to retrieve name and food_items of each category. MyDatabase { 'categories' : { 'category1'

Solution 1:

In your FoodCategory model, you mention that it has only SINGLE value of FoodItem:

publicclassFoodCategoryimplementsSerializable{
    String name;
    FoodItem food_items;
    ...
}

And by looking at your database structure, it should be a list/array of FoodItem right? And if your FoodItem only contains name string as key and boolean true as value (its always true, right?) then maybe you should make it HashMap, like this

publicclassFoodCategoryimplementsSerializable{
    String name;
    HashMap<String, Boolean> food_items;
    ...
}

That way, you don't need the FoodItem custom object. (or you can still use that and make food_items a list/array of FoodItem custom object.

EDIT

Looks like original poster magically "know" what to do and answer it himself (huh?). But for those who have the same problem and looking for answer here, this explanation is for you.

The database structure:

{"categories":{"category1":{"name":"Babyfood"},"category2":{"name":"Dairy"},"category3":{"food_items":{"item1":true,"item2":true},"name":"Fruits"}},"food_items":{"item1":{"category":"category3","name":"Apple"},"item2":{"category":"category3","name":"Banana"}}}

In the code sample that posted in question, it reads value on categories and search for categories/???/food_items for value. Please note that food_items I mention here is the ones inside the categories not food_items parent of item1 and item2.

So of course it will contain null for the first and third child as it has, in fact, no value. But the second one will have 2 values:

{"item1":true,"item2":true}

Note: it has 2 values, so I recommend using HashMap to read it, because original poster read it as single value.

From there, we know what is wrong. Then after that, you probably want to get detail of item1 and item2 because it only contains boolean (it is like a foreign key in sql). Then you should read value food_items/??? and there you go.

Solution 2:

For iterating all children of a node use ChildEventListener instead. Don't get confused by the method name 'onChildAdded' , it will go trough all entries the first time.

    mCategoryRef.addChildEventListener(newChildEventListener() {
        @OverridepublicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {
            FoodCategory c = dataSnapshot.getValue(FoodCategory.class);
            Log.d("Categories: ", c.name + " " + c.food_items);
            values.add(c);
        }

        @OverridepublicvoidonChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @OverridepublicvoidonChildRemoved(DataSnapshot dataSnapshot) {

        }

        @OverridepublicvoidonChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @OverridepublicvoidonCancelled(DatabaseError databaseError) {

        }
    });

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

Post a Comment for "How To Retrieve Data From Firebase Realtime Database In Android?"