코멘트를 저장할 오브젝트 배열이 있습니다. 올바른 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) {
}
});
}
'CommentNew' 클래스를 정의하십시오. – RamithDR
@RamithDR'CommentNew' 클래스를 게시합니다. – David
수업을 게시 해 주셔서 감사합니다. 내 대답 아래에 포함 된 솔루션을보십시오. – RamithDR