title: How to retrieve data from Firebase realtime database in Android?
body: <p>I am new here. I am not able to retrieve data from Firebase database. I want to retrieve name and <code>food_items</code> of each category.</p>
  
  <p><strong>MyDatabase</strong></p>
  
  <pre><code>{
      "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"
          }
      }
  }
  </code></pre>
  
  <p><strong>FoodCategory.java</strong></p>
  
  <pre><code>public class FoodCategory implements Serializable{
  
      String name;
      FoodItem food_items;
  
      public FoodCategory(){}
  
      public FoodCategory(String name,  FoodItem food_items) {
          this.name = name;
          this.food_items = food_items;
      }
  
      public FoodItem getFoodItems() {
          return food_items;
      }
  
      public String getName() {
          return name;
      }
  
      public FoodCategory(FoodCategory category){
          this.name = category.getName();
          this.food_items = category.getFoodItems();
      }
  
  }
  </code></pre>
  
  <p><strong>FoodItem.java</strong></p>
  
  <pre><code>public class FoodItem {
      String name;
  
      public FoodItem( FoodItem foodItem) {
          this.name = foodItem.getName();
      }
  
      public String getName() {
          return name;
      }
  }
  </code></pre>
  
  <p>Here is my code for retrieving data of each category</p>
  
  <pre><code>mRootRef = FirebaseDatabase.getInstance().getReference();
      mCategoryRef = mRootRef.child("categories");
      mCategoryRef.addValueEventListener(new ValueEventListener() {
  
          ArrayList&lt;FoodCategory&gt; values = new ArrayList&lt;&gt;();
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              for (DataSnapshot snapshot: dataSnapshot.getChildren()){
                  FoodCategory c = snapshot.getValue(FoodCategory.class);
                  Log.d("Categories: ", c.name + " " + c.food_items);
                  values.add(c);
              }
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  </code></pre>
  
  <p>It returns right value for category's name, but null for <code>food_items</code>.</p>