How to retrieve nested data from firebase

Viewed 71

This is the structure of data from my firebase:

Structure of data in firebase

I have problem to retrieve all "menukedai" data. By using the code below, data from "menukedai" didnt come out. For your information, the data of "menukedai" will come from the user.

This is my attempt:

foodDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads").child("menukedai");

foodDatabaseRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
            foodmenu foodmenu = postSnapshot.getValue(foodmenu.class);
            fooddata.add(foodmenu);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(foodlist.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

This is my foodmenu class.

package com.example.trialfoodbuddy;

public class foodmenu {
    private String Foodimage;
    private String Foodname;
    private CharSequence Foodprice;

    public foodmenu(){
        //empty constructor needed
    }
    public foodmenu(String foodimage, String foodname, CharSequence foodprice){
        Foodimage = foodimage;
        Foodname = foodname;
        Foodprice = foodprice;
    }

    public String getFoodimage(){
        return Foodimage;
    }

    public String getFoodname(){
        return Foodname;
    }

    public CharSequence getFoodprice(){
        return Foodprice;
    }

    public void setFoodimage(String foodimage){
        Foodimage = foodimage;
    }

    public void setFoodname(String foodname){
        Foodname = foodname;
    }

    public void setFoodprice(CharSequence foodprice){
        Foodprice = foodprice;
    }
}

1 Answers

I Believe you want to get the information nested under "menukedai" so you will need to add another loop for that information

foodDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
foodDatabaseRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot uploadSnapshot : dataSnapshot.getChildren()) {
            // this is where you actually getting the information 
            for(DataSnapshot menuItemSnapshot : uploadSnapshot.child("menukedai").getChildren()){
                foodmenu foodmenu = menuItemSnapshot.getValue(foodmenu.class);
                fooddata.add(foodmenu);
            }
        }
    }
    // Log the values and check them to make sure you getting the data you want if always nice so I added it for you here
    Log.i("Values","My values are:"+foodmenu);
    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(foodlist.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

Normally speaking, You should always use the variable names in your class in your Database so when you pulling the information, your constructor would know what value assign to which variable in your class.

Related