Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 980
Firas Chebbah Avatar asked Feb 02 '26 13:02

Firas Chebbah


1 Answers

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"
like image 81
Alex Mamo Avatar answered Feb 04 '26 02:02

Alex Mamo