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?
If you open your app from Firebase dashboard, you can add data manually by clicking on the + sign.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With