Adding a list inside a node in firebase

saad bitar :

I'm having trouble lately adding the json structure i want into firebase database. I want to add an extra attribute to my database like in the image enter image description here

I tried orderstReference.child(pid).child("quantity").setValue(orderId); but the value is overwriting each time its execute where i want them to add like in a list.

How can i add this ? and is there any useful link to learn these stuff i can't find what i want?

Peter Haddad :

If you already have a node with data, and you want to add an extra child then using setValue() will override the whole node. In this case, you need to use 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);
}

Check this link:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360262&siteId=1