サアド・ビット:
ピーター・ハダッド:
すでにデータを持つノードを持っていて、使用して余分な子を追加したい場合はsetValue()
、全ノードを上書きします。このケースでは、使用する必要がありますupdateChildren()
:
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);
}
このリンクをチェックしてください:
https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields