简体   繁体   中英

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 . 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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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