我在android中使用firebase向聊天组添加消息然后将其检索回来但是当我按下发送时它会添加新消息并删除之前添加的其他子(ren)消息实例我只保留一条消息,即新消息。解决这个问题的任何建议。
资本谈话和高峰时间是我的组(代码中的程序名)的示例,我想将消息添加为子(ren),其中包含push生成的唯一ID,因为您只能看到一条消息被保留 设置Firebase
Firebase.setAndroidContext(this);
mQuery = new Firebase(Constants.FIREBASE_URL+"/"+Constants.KEY_CHATS+"/"+programname);
mQuery.orderByChild("timestamp").limitToLast(100);
Adding new node
final EditText ed_post = (EditText) findViewById(R.id.ed_post);
ImageButton btn_post = (ImageButton) findViewById(R.id.btn_post);
btn_post.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String post = ed_post.getText().toString();
if (!post.isEmpty()) {
// chat.sendPost(post, program_id, station_id, user_id,
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
Firebase chatbase =new Firebase(Constants.FIREBASE_URL+"/"+Constants.KEY_CHATS);
chatbase.child(programname).setValue(programname);
Firebase msgbaseref =new Firebase(Constants.FIREBASE_URL+"/"+Constants.KEY_CHATS+"/"+programname);
ChatsManager chm=new ChatsManager(username,post,timeStamp,"1",Constants.KEY_LIKES,Constants.KEY_COMMENTS);
msgbaseref.push().setValue(chm);
ed_post.setText("");
init();
}
retrieving results/data to recycler view
reycprogramlist.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
reycprogramlist.setLayoutManager(mLayoutManager);
mAdapter = new FirebaseRecyclerAdapter<ChatsManager, ChatMessagesViewholder>(ChatsManager.class, R.layout.chat_list_item, ChatMessagesViewholder.class, mQuery) {
@Override
protected void populateViewHolder(ChatMessagesViewholder holder, ChatsManager item, int i) {
holder.tv_post.setText(item.getMessage());
holder.tv_time.setText(String.valueOf(item.getTimestamp()));
holder.tv_username.setText(String.valueOf(item.getUsername()));
// holder.tv_time.setText(String.valueOf(item.getTimestamp()));
if(username.equalsIgnoreCase(item.getUsername())){
holder.ll.setBackgroundResource(R.drawable.right_chat);//iv_right.setImageBitmap(letterTile);
}else {
holder.ll.setBackgroundResource(R.drawable.left_chat);//iv_right.setImageBitmap(letterTile);
}
}
};
ChatManager class
public class ChatsManager {
private String programname;
private String username;
private String message;
private String timestamp;
private String status;
private String avatar;
private String likes;
private String comments;
public ChatsManager(){}
public ChatsManager(String username,String message, String timestamp, String status,String likes,String comments) {
this.username = username;
this.message = message;
this.timestamp = timestamp;
this.status = status;
this.likes = likes;
this.comments = comments;
}
public ChatsManager(String username){
this.username = username;
}
public String getLikes() {
return likes;
}
public String getComments() {
return comments;
}
public String getProgramname() {
return programname;
}
public String getUsername() {
return username;
}
public String getMessage() {
return message;
}
public String getTimestamp() {
return timestamp;
}
public String getStatus() {
return status;
}
public String getAvatar() {
return avatar;
}
}
Chats Json
{
"chats" : {
"Breakfast Show" : {
"ligaye" : {
"comments" : "comments",
"likes" : "likes",
"message" : "ghhh",
"status" : "1",
"timestamp" : "2016.04.28.22.13.54",
"username" : "ligaye"
}
},
"Capital talk" : {
"-KGStyEtP0GxxmDu99Gu" : {
"comments" : "comments",
"likes" : "likes",
"message" : "kumeflood",
"status" : "1",
"timestamp" : "2016.04.28.20.25.58",
"username" : "ligaye"
}
},
"Capital talk2" : "Capital talk2",
"Rush hour" : {
"-KGDkn-PaKdnN3D9pFaX" : {
"comments" : "comments",
"likes" : "likes",
"message" : "not working again",
"status" : "1",
"timestamp" : "2016.04.25.21.51.34",
"username" : "ligaye"
}
}
[This is how my chats structure looks like][1]
[1]: http://i.stack.imgur.com/SvHIX.png