Firas Chebbah
Firas Chebbah

Reputation: 434

How to push data in array stored in the Firebase using Android?

I need to add data to an array stored in the database to be like this: places:

0:"new Item"

1:"new Item",

2:"new Item"

.

.

.

My problem is How to push data in array stored in the Firebase without the need to use a Hashmap? For exemple the next data pushed will result on:

places: 0:"old Item"

1:"vItem",

2:"old Item",

3:"new Item"

I know that if i use the method given bellow, the data will be erased and a new data set will be added,

Utils.eventsRef.child(events.getUid()).child("arrayTicket").setValue(str);

Can someone help me please ?

Upvotes: 3

Views: 4503

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

According to your comments, to be able to add an array into your Firebase realtime database, you need first to convert your array to a List like in the following lines of code:

String[] items = {"newItem", "newItem", "newItem"};
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference arrayTicketRef = rootRef.child("arrayTicket");
arrayTicketRef.setValue(Arrays.asList(items));

The result in your Firebase console will be:

Firebase-root
   |
   --- arrayTicket
         |
         --- 0: "newItem"
         |
         --- 1: "newItem"
         |
         --- 2: "newItem"

Upvotes: 2

Related Questions