Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何更新Firebase中的对象数组?_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

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

Android 如何更新Firebase中的对象数组?,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我有一个保存注释的对象数组。我想找到一个正确的postid,并在用户留下评论时向该数组插入一个新对象 模式 "comments" : { "-KfXyMUIqsiPGElax098" : { "comments" : [ { "author" : "s123456", "comment" : "This is the first comment." } ], "postid" : "-KfSToVpsWUecTL_tmlh" }

我有一个保存注释的对象数组。我想找到一个正确的
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) {

        }
    });
}
public void saveComment(字符串postIdKey,final CommentNew CommentNew){
queryqueryref=mFirebase.child(“comments”).orderByChild(“postid”).equalTo(postIdKey);
addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
对于(DataSnapshot快照:DataSnapshot.getChildren()){
String key=snapshot.getKey();
String path=“comments/”+key+“/comments”;
List comment=newarraylist(Arrays.asList(commentNew));
映射结果=新的HashMap();
结果。放置(“注释”,新注释);
mFirebase.child(路径).updateChildren(结果);
}
}
@凌驾
取消后的公共无效(FirebaseError FirebaseError){
}
});
}

您所处的路径对于更新数据是正确的,但您缺少几个步骤

第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;
}

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

或者,您可以将
OnCompleteListener
添加到
updateChildren
方法中,并向用户敬酒,表示评论已成功更新或您喜欢的任何内容

补充案文:


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

请发布您的
CommentNew
类定义。@RamithDR我发布了
CommentNew
类。谢谢发布该类内容。试试下面我的答案中包含的解决方案。这不起作用。因为新的模型存储为数组。看一看。另外,旧数据被新数据替换。似乎将注释保存为数组不是一个好主意。也许,我应该换个房间structure@David没错,像这样的嵌套数据更难维护。如果你能用你的项目场景更新这个问题,我也许能在数据库结构方面帮助你。
@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);
   }
}