0

Update : recyclerview , detail

How can I retrieve children of child "produit" !! I want to display it in RecyclerView.

This is the code how I save data in 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);

enter image description here

1 Answers1

3

Since you can have multiple produitcommande1, produitcommande2, and so on, a more feasible structure would be to add the products in Map, as seen below:

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

In this way, you can add as many products as you want. To read them, simply get the node as a Map object and integrate it through the records and only get the keys. If you want to have the produitcommande as a field in a class, then simply add it as a Map<String, Object>.

According to your last comment:

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
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hey, I still trying to fix it by putting products in Map – Mohamed Yacine Zouaoui Nov 16 '21 at 08:32
  • There is another problem, The client have to define the quantity of each product, so how I do ? – Mohamed Yacine Zouaoui Nov 16 '21 at 08:33
  • Instead of adding the value of `true`, add the corresponding quantity as in my updated answer. Is it ok now? – Alex Mamo Nov 16 '21 at 08:54
  • I have never used the "Map" method so I will try it and tell u the answer. Also I update the poste by adding a picture, can u tell me which structure is the best !! between my two suggestions and yours please !! – Mohamed Yacine Zouaoui Nov 16 '21 at 09:02
  • **None** of your solutions solves the problem. You cannot have dynamic fields (1, 2,3 ...). The solution I provided you, solves the problem as it contains the product as well as the quantity, right? – Alex Mamo Nov 16 '21 at 09:11
  • Okay, I will do it. Thank you – Mohamed Yacine Zouaoui Nov 16 '21 at 09:14
  • Give it a try and tell me if it works. Looking forward to seeing your success. – Alex Mamo Nov 16 '21 at 09:21
  • Hi I changed the structure of my database as you told me. But I don't know how to use Map method to get the data ( D2 : 10 , D3 : 15) from the database ! – Mohamed Yacine Zouaoui Nov 17 '21 at 12:48
  • Simply by using this solution in this [answer](https://stackoverflow.com/a/46908/5246885), right? – Alex Mamo Nov 17 '21 at 12:52
  • Right. But in my case I don't show the products in the first recyclerview(which contains "nompdv","date","etat")[reyclerview]*pic in the post* , I want to get it in another activity [detail]*pic in the post* when I click on the item. You understand me ?? – Mohamed Yacine Zouaoui Nov 17 '21 at 13:02
  • Yes, then simply pass **only** that Map to the next activity and use an adapter to display the products and the corresponding quantities. Iterate the Map, add those elements to a List, pass that List to an adapter and that's it, right? – Alex Mamo Nov 17 '21 at 13:06
  • Should I add a list for the products inside the first recyclerview ? If yes I give it a key to pass data to the next activity and I use an adapter to display it ? right or !!? – Mohamed Yacine Zouaoui Nov 17 '21 at 13:15
  • As I understand, in the first activity you display `"nompdv","date","etat"` and the second you want to display the products with the quantities. Then to the second activity simply just pass the Map, as the Map contains the products and quantities. The key is the product and the value is the quantity. – Alex Mamo Nov 17 '21 at 13:17
  • Yes like that, in the second activity I want to display all the children "nompdv","date"...and the products also. Then how i retrieve the products from database into the map inside the recyclerview !!? – Mohamed Yacine Zouaoui Nov 17 '21 at 13:27
  • Hey, should I change the way how to save data in firebase ? – Mohamed Yacine Zouaoui Nov 17 '21 at 14:38
  • Yes, as I already explained in my answer. – Alex Mamo Nov 17 '21 at 15:17
  • If you have some hard time implementing that mechanism, please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Nov 17 '21 at 15:19
  • 1
    Okay, I will do, thank you for ur help – Mohamed Yacine Zouaoui Nov 17 '21 at 15:36