简体   繁体   English

如何在Firebase上获取节点名称?

[英]How can i get a node name on firebase?

I'm making a menu for a restaurant, first of all I want to know if this structure is correct. 我正在为餐厅制作菜单,首先我想知道这种结构是否正确。

在此处输入图片说明

 "menu" : {
    "Steak taco" : {
      "Description" : "Roasted steak with chipotle sauce taco.",
      "Price" : 5
    }
  }

if its not, how can i improve it. 如果没有,我该如何改善。 Second, i want to retrive child menu name. 其次,我要检索子菜单名称。 for example, i want to get Steak taco name, the description and price 例如,我想获取牛排炸玉米饼的名称,描述和价格

 DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
        final DatabaseReference refNom = ref.child("Locales");

final DatabaseReference refNom = ref.child("Locales");
            refNom.orderByChild("Nombre").equalTo(nombreLocal/*i'm getting this from a getExtra*/).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    lista.removeAll(lista);
    for (DataSnapshot ds1 : dataSnapshot.getChildren()) {

    String nomPlatillo = ds1.child("menu").getKey();
    String descPlatillo = ds1.child("menu")./*dish_name.child*/child("Description").getValue(String.class);
    double precio = ds1.child("menu")./*dish_name.child*/child("price").getValue(double.class);
    datosMenu menu = new datosMenu(nomPlatillo,descPlatillo,precio);
    lista.add(menu);
}

This is my loop: 这是我的循环:

Since menu will have lot of items, I guess it would be better if you can create like array of items 由于菜单中会有很多项目,所以我想如果可以创建类似的项目数组会更好

{
  "menu": [
    {
      "item": "Steak taco",
      "desc": "",
      "price": "5",
      "image_url": ""
    },{
      "item": "pizza",
      "desc": "",
      "price": "4",
      "image_url": ""
    }
  ]
}

This way it would be easy to iteratively parse. 这样,很容易迭代解析。

You are missing a child. 你想念一个孩子。 Between your menu node and the Description key there is one more step in the tree hierarchy, Steak taco . menu节点和Description键之间,树层次结构中还有一个步骤Steak taco So to solve this, please change the following lines of code: 因此,为解决此问题,请更改以下代码行:

String descPlatillo = ds1.child("menu")./*dish_name.child*/child("Description").getValue(String.class);
double precio = ds1.child("menu")./*dish_name.child*/child("price").getValue(double.class);

to

String descPlatillo = ds1.child("menu").child("Steak taco").getValue(String.class);
double precio = ds1.child("menu").child("Steak taco").getValue(Double.class);

Please also note that is have used getValue(Double.class) and not getValue(double.class) . 另请注意,已使用getValue(Double.class)而不是getValue(double.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM