Tony Baby Tony Baby - 2 months ago 16
Android Question

How to add data to firebase database from android application

I have an android application that uses firebase for storing data. I want to add new data to the database without overwriting the exiting value.

My code

Firebase ref = new Firebase(Constants.FIREBASE_APP + "/id");
ref.child("Data").setValue(possibleValue);


I know this will overwrite the current value. I want to know how to use push() method. Please help

Answer

Check the doc:

Using setValue() in this way overwrites data at the specified location, including any child nodes.

You should use the push() method to generate a unique ID every time a new child is added to the specified Firebase reference.

Code (from the comments):

Person person = new Person(); //Adding values person.setName(name); 
person.setAddress(address); 
Firebase newRef = ref.child("Person").push(); 
newRef.setValue(person);