Writing data to firebase through an Android application

I am new to firebase and am trying to use this as a backend for an Android app to store data. The data format is a key, a pair of values.

This is the code I use to store data:

Map<Integer, PersonData> map = new HashMap<Integer, PersonData>(); map.put(PersonData.getID(), new PersonData("abcd", 12345)); Firebase ref = new Firebase(url).push(); ref.setValue(map); 

Due to the click link used, data is saved as follows:

 -J5upSABqTLJ1Wfu-jFq 12345 id: 12345 name: abcd 

Where - how do I want the data to be stored as follows:

 12345 id: 12345 name: abcd 

I'm not quite sure that the code example above is the right way to store data. Because I want to be able to update existing data at a later point in time. Any suggestions?

EDIT 1: I think I need to use push so that I don't overwrite existing data in the firebase repository. I just tried to return the data using the getValue () method, and I can only retrieve the data that is in the MAP

EDIT 2: without using the push () method with my link, I see that any previous data is overwritten and only the latest information is available. I wonder if they are the best way to get the link and still retain the previous information.

+7
android firebase
source share
2 answers

So it looks like you have your own unique identifier system, in which case you do not need to use the .push method (this is just a helper to get a unique ref for new data). Therefore, instead of a push, you should be able to:

 Map<Integer, PersonData> map = new HashMap<Integer, PersonData>(); map.put(PersonData.getID(), new PersonData("abcd", 12345)); Firebase ref = new Firebase(url).child("12345"); ref.setValue(map); 

Assuming your id is "12345" and url points to the place where you want to keep all your persons .

To update data without overwriting, your ref will look like this:

 Firebase ref = new Firebase(url).child("12345"); 

And instead of using .setValue you would like to use ref.updateChildren(updates) . You can see how to structure updates from an example in docs :

 Map<String, Object> updates = new HashMap<String, Object>(); updates.put("first", "Fred"); updates.put("last", "Swanson"); nameRef.updateChildren(updates); 
+16
source share

Be sure to enable permission for the project to use the Internet in the manifest.

 <uses-permission android:name="android.permission.INTERNET"/> 
-5
source share

All Articles