如何更新Firebase中的对象数组?

时间:2017-03-19 17:06:32

标签: android firebase firebase-realtime-database

我有一组对象来保存评论。我想找到一个正确的postid,并在用户发表评论时向该数组插入一个新对象。

模式

"comments" : {
  "-KfXyMUIqsiPGElax098" : {
     "comments" : [ {
       "author" : "s123456",
       "comment" : "This is the first comment."
      } ],
     "postid" : "-KfSToVpsWUecTL_tmlh"
  }
}

模型

public class CommentNew {

String author;
String comment;

public CommentNew() {}

public CommentNew(String author, String comment) {
    this.author = author;
    this.comment = comment;}

public String getAuthor() {
    return author;}

public void setAuthor(String author) {
    this.author = author;}

public String getComment() {
    return comment;}

public void setComment(String comment) {
    this.comment = comment;}
}

代码

我找到了正确的postid,但是如何将新对象附加到数组?

    public void saveComment(String postIdKey, final CommentNew commentNew) {
    Query queryRef = mFirebase.child("comments").orderByChild("postid").equalTo(postIdKey);
    queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
                String key = snapshot.getKey();
                String path = "comments/" + key + "/comments";
                List comment = new ArrayList<CommentNew>(Arrays.asList(commentNew));
                Map<String, Object> result = new HashMap<String, Object>();
                result.put("comments", commentNew);
                mFirebase.child(path).updateChildren(result);
            }
        }
        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
}

1 个答案:

答案 0 :(得分:0)

您所使用的路径更新数据是正确的,但您缺少一些步骤。

第1步:

您的CommentNew类应该包含将CommentNew对象转换为HashMap的方法。像这样:

public class CommentNew {

String author;
String comment;

public CommentNew() {}

public CommentNew(String author, String comment) {
    this.author = author;
    this.comment = comment;}

public String getAuthor() {
    return author;}

public void setAuthor(String author) {
    this.author = author;}

public String getComment() {
    return comment;}

public void setComment(String comment) {
    this.comment = comment;}

//converts an object of CommentNew to a HashMap
@Exclude
public Map<String, Object> toMap() {

   HashMap<String, Object> result = new HashMap<>();
   result.put("author", getAuthor());
   result.put("comment", getComment());

   return result;
}

}

第2步:

onDataChange中的代码更改为以下内容:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
   for (DataSnapshot snapshot: dataSnapshot.getChildren()) {

       String key = snapshot.getKey();       
       String path = "comments/" + key + "/comments";

       Map<String, Object> updatedComment = commentNew.toMap();
       Map<String, Object> childUpdate = new HashMap<>();
       childUpdate.put(path, updatedComment);

       mFirebase.updateChildren(childUpdate);
   }
}

如您所见,使用了两个HashMaps,第一个HashMap用于存储CommentNew对象的更新属性列表,第二个HashMap包含子应更新的路径+更新的数据。

您可以选择向OnCompleteListener方法添加updateChildren,并向用户提供评论已成功更新或您喜欢的任何内容。

补充阅读:Update specific fields

HTH,如果您需要进一步澄清,请在下方发表评论;