To write single data you can use the setValue()
method on your DatabaseReference
with your child Id's:
private void writeNewData(String userId, String name, String email) {
User user = new User(name, email);
mDatabase.child("users").child(userId).setValue(user);
}
In your case you can do something like:
mDatabase.child("UID2").child("KEY2").setValue(yourNewValueOrObject);
If you want to update a specific value, you should be more concise:
mDatabase.child("UID2").child("KEY2").child("email").setValue(newEmail);
Anyway I recomend you to use custom classes as POJO's(Plain Old Java Object) with the values of each of your items in database. For example:
public class User {
public String username;
public String email;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
}
Finally to remove data you should use the removeValue()
method in the same way.
private void deleteUserData(String userId) {
mDatabase.child("users").child(userId).removeValue();
}
This method will remove the whole reference from your Database, so be care with it. In the case that you wanted to remove a specific field, you should add another .child()
call to the tree. For example, let's say that we want to remove the email value from "KEY2" node:
mDatabase.child("users").child(userId).child("email").removeValue();
Finally, there's the case that maybe we want to update multiple fields in different database nodes. In that case we should use the updateChildren()
method with a map of references and values.
private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
}
What updateChildren
method do. Is a setValue ()
call over each row in the given Map<String, Object>
being the key the full reference of the node and the Object the value.
You can read more update and delete data in the official Firebase documentation