Bitlejuce Do
Bitlejuce Do

Reputation: 340

Firebase: How to set value to child without updating/deleting subChildren?

I was trying to change values in child of my DB, but can't do it whithout deleting other data under this child. Trying to use push(), but it create new child and I want update value of existing child and save other childs.

enter image description here

Let's say I need to change ONLY value of Field 1 (email, name) and save child Test and other data under.

 databaseRef.child("Field 1").setValue(new UserForRealtimeDatabase("1st value of Field 1", "2st value of Field 1"));

This code is work but all the data under Field 1 complitely delete. Can you help me how to do that or may be there is no standart option for that?

Upvotes: 2

Views: 5446

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80944

You need to do the following:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child("Field 1");
Map<String, Object> updates = new HashMap<String,Object>();
updates.put("email", newemail);
updates.put("name", newname);

ref.updateChildren(updates);

more info here:

https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields

public Task<Void> updateChildren (Map<String, Object> update)

Update the specific child keys to the specified values. Passing null in a map to updateChildren() will remove the value at the specified location.

Upvotes: 6

Related Questions