如何在 firebase 中检索子(自动递增)名称

How to retrieve child (auto-increment) name in firebase

更新: recyclerview , detail

如何检索子“produit”的子项!! 我想在 RecyclerView 中显示它。

这是我在 Firebase 中保存数据的代码:

long OrderNum = 1;

DatabaseReference newPost = reference.child(refernce);
newPost.child("refernce").setValue(refernce);
newPost.child("nompdv").setValue(nompdv);
newPost.child("etat").setValue(etat);
newPost.child("datecommande").setValue(datecommande);
newPost.child("user_id").setValue(uid);

DatabaseReference newOrder = reference.child(refernce).child("produit");
newOrder.child(produitcommande).setValue(qntcommande);

由于您可以有多个produitcommande1、produitcommande2等,更可行的结构是将产品添加到Map中,如下所示:

Firebase-root
  |
  --- commande
        |
        --- KAKJQBUX
              |
              --- datecommande: "10-11-2021 15:15"
              |
              --- produitcommande
                    |
                    --- D2: true
                    |
                    --- D3: true

这样,您可以添加任意数量的产品。要读取它们,只需将节点作为 Map 对象获取并通过记录将其集成并仅获取键。如果您想将 produitcommande 作为 class 中的字段,则只需将其添加为 Map.

根据您最后的评论:

The client has to define the quantity of each product, so how do I do it?

Firebase-root
  |
  --- commande
        |
        --- KAKJQBUX
              |
              --- datecommande: "10-11-2021 15:15"
              |
              --- produitcommande
                    |
                    --- D2: 2
                    |
                    --- D3: 5