Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new data in firebase android

This is my logic for adding new person in firebase realtime databse. But instead of making a new entry it is just updating the old data with new one.

buttonSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            /*
            new Firebase(Config.FIREBASE_URL)
                    .push()
                    .child("title")
                    .setValue(text.getText().toString());
            */

            Firebase ref = new Firebase(Config.FIREBASE_URL);
            String name = editTextName.getText().toString().trim();
            String address = editTextAddress.getText().toString().trim();

            //Creating Person object
            Person person = new Person();

            //Adding values
            person.setName(name);
            person.setAddress(address);
            ref.child("Person").setValue(person);

        }
    });


    new Firebase(Config.FIREBASE_URL).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                //Getting the data from snapshot
                Person person = postSnapshot.getValue(Person.class);

                //Adding it to a string
                String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\n\n";

                //Displaying it on textview
                textViewPersons.setText(string);
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });

What is wrong here? Can anyone help me on this?

like image 312
user3559471 Avatar asked Jun 01 '16 06:06

user3559471


People also ask

How do I manually add data to Firebase?

If you open your app from Firebase dashboard, you can add data manually by clicking on the + sign.

How to upload data in Firebase?

To upload a file to Cloud Storage, you first create a reference to the full path of the file, including the file name. Once you've created an appropriate reference, you then call the putBytes() , putFile() , or putStream() method to upload the file to Cloud Storage.


2 Answers

You are using always the same ref

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

Check the doc:

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

In your case you are overriding the same data for this reason.

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

 Person person = new Person();
 //Adding values
 person.setName(name);
 person.setAddress(address);
 DatabaseReference newRef = ref.child("Person").push();
 newRef.setValue(person);
like image 78
Gabriele Mariotti Avatar answered Sep 30 '22 10:09

Gabriele Mariotti


You can add a new Child to your data, such as:

mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
String userId = user.getUid();
Firebase rootRef = new Firebase(Config.USER_URL);
Firebase userRef = rootRef.child("Users");
Person newUser = new Person();
newUser.setFirstName(firstName);
newUser.setLastName(lastName);
userRef.child(userId).setValue(newUser);

The userId varies when the logging user is different, therefore, you will always go to a new data list for a new logged-in-user in the userId under Users.

like image 29
Loyal Fine Avatar answered Sep 30 '22 09:09

Loyal Fine