如何从Android中的Firebase实时数据库中检索数据? [英] How to retrieve data from Firebase realtime database in Android?

查看:40
本文介绍了如何从Android中的Firebase实时数据库中检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.我无法从Firebase数据库检索数据.我想检索每个类别的名称和 food_items .

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" : {
          "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"
        }
    }
}

FoodCategory.java

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

}

FoodItem.java

public class FoodItem {
    String name;

    public FoodItem( FoodItem foodItem) {
        this.name = foodItem.getName();
    }

    public String getName() {
        return name;
    }
}

这是我用于检索每个类别的数据的代码

Here is my code for retrieving data of each category

mRootRef = FirebaseDatabase.getInstance().getReference();
    mCategoryRef = mRootRef.child("categories");
    mCategoryRef.addValueEventListener(new ValueEventListener() {

        ArrayList<FoodCategory> values = new ArrayList<>();
        @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) {

        }
    });

它为类别名称返回正确的值,而对于 food_items 返回空值.

It returns right value for category's name, but null for food_items.

推荐答案

在您的 FoodCategory 模型中,您提到它只有 FoodItem :

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

public class FoodCategory implements Serializable{
    String name;
    FoodItem food_items;
    ...
}

通过查看您的数据库结构,它应该是 FoodItem 的列表/数组,对吗?而且,如果您的 FoodItem 仅包含名称字符串作为键,而布尔值true作为值(它始终为true,对吗?),那么您应该像这样将其设置为 HashMap

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

public class FoodCategory implements Serializable{
    String name;
    HashMap<String, Boolean> food_items;
    ...
}

这样,您就不需要 FoodItem 自定义对象.(或者您仍然可以使用它,并将 food_items 设置为 FoodItem 自定义对象的列表/数组.

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.

编辑

看起来就像原始的海报一样神奇地知道"该怎么做并亲自回答(是吗?).但是对于那些有相同问题并在此处寻求答案的人,此说明适合您.

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.

数据库结构:

{
    "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"
        }
    }
}

在有问题的代码示例中,它读取 categories 上的值,并在 categories/???/food_items 中搜索值.请注意,我在这里提及的 food_items item1 的父级的 food_items 父级,而不是 food_items 父级item2 .

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.

因此,它的第一个和第三个孩子当然将包含 null ,因为它实际上没有任何价值.但是第二个将具有2个值:

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 }

注意:它有2个值,所以我建议使用 HashMap 读取它,因为原始海报将其读取为单个值.

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

从那里,我们知道出了什么问题.然后,在那之后,您可能想获取 item1 item2 的详细信息,因为它仅包含布尔值(就像sql中的外键一样).然后,您应该读取值 food_items/??? ,然后就可以了.

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.

这篇关于如何从Android中的Firebase实时数据库中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆