Gastón Saillén
Gastón Saillén

Reputation: 13129

Trouble adding different keys in Firebase database

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

Upvotes: 0

Views: 65

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

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.

Upvotes: 2

Related Questions