2017-03-19 6 views
1

コメントを保存するオブジェクトの配列があります。私は正しいpostidを見つけて、ユーザーがコメントを残すたびにその配列に新しいオブジェクトを挿入したいと思います。Firebaseでオブジェクトの配列を更新するには?

スキーマ

"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) { 

     } 
    }); 
} 
+0

あなたの 'CommentNew'クラスを投稿してください。 – RamithDR

+0

@RamithDR 'CommentNew'クラスを投稿します。 – David

+0

クラスを投稿してくれてありがとう。下の私の答えに含まれているソリューションをお試しください。 – RamithDR

答えて

0

あなたが行っているパスはデータを更新するのに間違いありませんが、いくつかの手順がありません。

ステップ1:

あなたCommentNewクラスはHashMapのにCommentNewオブジェクトを変換する方法を含める必要があります。

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

使用すると、2つのハッシュマップが使用された見ることができるように、最初のHashMapをするために使用されますCommentNewオブジェクトの更新された属性リストを格納し、2番目のHashMapには、更新する必要があるパスと更新されたデータが格納されます。

updateChildrenメソッドにOnCompleteListenerを追加し、コメントが正常に更新されたこと、または好きなものをToastに通知することもできます。

追加読み込み:Update specific fields

HTH、あなたが任意の更なる明確化が必要な場合は、以下のコメント。

+0

これは動作しません。 'CommentNew'モデルは配列として格納されるためです。 http://i.imgur.com/mQyv91j.pngを見てください。古いデータは新しいデータに置き換えられます。 – David

+0

配列としてのコメントを保存するのは良い考えではないようです。おそらく、私は構造体を変更する必要があります – David

+0

@Davidそれは、そのようなネストされたデータを維持することは難しいです。プロジェクトのシナリオで質問を更新できるのであれば、データベース構造を手伝うことができます。 – RamithDR

関連する問題