0

I'm struggling with a simple thing. I need to push different card numbers into Firebase database, each card number have their own offer, so I need to push different offers to the same card. This is done by using a HashMap(), so, what I want is to be able to add different card numbers as different keys, and inside those card numbers different offers as different keys without overriding it.

For now this is what I have and what I want. The offers inside each card should be different, lets say ofert1, ofert2 and so on, and the card numbers also. If I add the same card number to the editText I use to pass the data it should go to the same card and be able to create offers inside that card.

Database Structure

The problem is if I continue adding data the old data get replaced with the new one, I just want to be able to still adding different card numbers with different offers inside the same card.

What I have done is this

 Map mOffers= new HashMap();
             mOffers.put("codigo_oferta",mStringCodigoOferta);
 Map mCardNumbers= new HashMap();
             mCardNumbers.put(mStringTarjeta, mOffers);
                 mDatabase.child("Locales").child(uid).setValue(mCardNumbers);

Updates

What I want is like the push() with different keys, but pushing different card numbers and different offers inside each card number.

Also, if I add push() to the reference it will push different cards, but with a random key, and I don't want it, I want the card to be the different keys that stores different offers.

enter image description here

Edit 2: now it's working like Frank's answer, but instead of keep adding offers I'm getting them replaced.

Structure

halfer
  • 19,824
  • 17
  • 99
  • 186
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77

1 Answers1

2

I think you're asking for this:

Map mOffers= new HashMap();
mOffers.put("ofert1", true);
mOffers.put("ofert2", true);
Map mCardNumbers= new HashMap();
mCardNumbers.put("45672224", mOffers);

mDatabase.child("Locales").child(uid).setValue(mCardNumbers);

Or more directly:

Map mOffers= new HashMap();
mOffers.put("ofert1", true);
mOffers.put("ofert2", true);
mDatabase.child("Locales").child(uid).child("45672224").setValue(mOffers);

If you want to add an existing item to the offers, you can call setValue on that specific path:

mDatabase.child("Locales").child(uid).child("45672224/ofert3").setValue(true);

If you want to add/change/remove multiple child nodes, but leave others unmodified, user updateChildren(). For example, this adds ofert3 and removes ofert2:

Map mOffers= new HashMap();
mOffers.put("ofert3", true);
mOffers.put("ofert2", null);
mDatabase.child("Locales").child(uid).child("45672224").updateChildren(mOffers);

Note that this point I'm repeating much of what is explained in the Firebase documentation on reading and writing data and working with lists of data, so I recommend spending some time on those pages.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks so much frank ! it worked like i want ! , now, if i dont want to see the true when i create the ofert1 , is there a way to not have it and just the ofert ? thanks so much ! – Gastón Saillén Mar 15 '18 at 15:40
  • Also , when i put the same card number but different offer, the offer is replaced by the new one instead of keep adding it – Gastón Saillén Mar 15 '18 at 15:41
  • There is no way to keep a key without a value in the Firebase Database, same as that isn't possible in JSON. Just consider the `true` a marker value, something needed to make it valid. Whatever you do, **don't** change the structure to array. While visually that may look closer that what you want, it's the wrong data structure for what is essentially a mathematical set. For more on that, see my answer [here](https://stackoverflow.com/q/40656589). – Frank van Puffelen Mar 15 '18 at 16:50
  • Thanks Frank, but let's say I want to add multiple keys inside the card number without being replaced , is that the same thing as doing the first map for the card numbers ? Or I need to do another step with the map in order to accomplish that – Gastón Saillén Mar 15 '18 at 17:02
  • I need to add multiple offers inside the card number without being replaced – Gastón Saillén Mar 15 '18 at 17:03
  • I'm doing this in order to let my client see and understand the database because he dosnt want an administrator for his app , I told him to do a firebase recycler adapter in order to view the data structured, but he told me that he wants to see the data from the browser, so I'm trying to structure the data in a way he can read it without navigate too much into the database, let's say I want to make this database easy to read for people that never peogrammed – Gastón Saillén Mar 15 '18 at 17:07
  • I added some more examples for updating. On your latest comment: never trade a proper data model for one that is easier to read for somebody else. If needed consider making a simple web admin page that shows the data in the format your client needs. – Frank van Puffelen Mar 15 '18 at 17:12