一如既往地感谢您的帮助。我正在制作一个Firebase聊天应用程序,遇到了这个错误,我可以弄清楚发生了什么。
通常,如果我清除数据库并重新启动,则不会发生错误,但是 每当我发送消息时,recyclerview的新消息中都会包含较旧的接收消息。我迷路和沮丧。我已经在stackoverflow上搜索了没有积极结果的解决方案。这是错误的屏幕截图。
private void loadMessages() {
chatMessagesRef = db.getReference("ChatMessages").child(Common.currentUser.getMatric()).child(user_id);
adapter = new FirebaseRecyclerAdapter<Message, ChatMessageViewHolder>(
Message.class,
R.layout.single_message_item,
ChatMessageViewHolder.class,
chatMessagesRef
) {
@Override
protected void populateViewHolder(final ChatMessageViewHolder viewHolder, final Message model, int position) {
if (model.getFrom().equalsIgnoreCase(Common.currentUser.getMatric())) {
if (model.getType().equalsIgnoreCase("text")){
viewHolder.yourMessage.setText(model.getMessage());
viewHolder.yourMessage.setVisibility(View.VISIBLE);
viewHolder.theAll.setBackgroundColor(Color.WHITE);
}
} else if (model.getFrom().equalsIgnoreCase(user_id)){
if (model.getType().equalsIgnoreCase("text")){
viewHolder.messageText.setText(model.getMessage());
viewHolder.messageText.setVisibility(View.VISIBLE);
viewHolder.theAll.setBackgroundColor(Color.YELLOW);
}
}
}
};
messagesRecycler.setAdapter(adapter);
adapter.notifyDataSetChanged();
messagesRecycler.scrollToPosition(adapter.getItemCount() - 1);
adapter.cleanup();
}
private void sendTheMessage() {
String message = chatBox.getText().toString();
if (message.isEmpty()){
} else {
long date = System.currentTimeMillis();
messageRef = db.getReference("ChatMessages").child(Common.currentUser.getMatric()).child(user_id).push();
final String chatMessageId = messageRef.getKey();
final Message newMessage = new Message(message, "text", date, false, Common.currentUser.getMatric());
messageRef2 = db.getReference("ChatMessages").child(Common.currentUser.getMatric()).child(user_id);
messageRef2.child(chatMessageId).setValue(newMessage).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
messageRef3 = db.getReference("ChatMessages").child(user_id).child(Common.currentUser.getMatric());
messageRef3.child(chatMessageId).setValue(newMessage);
sendNotification();
chatBox.setText("");
}
});
}
}
公共类消息{
private String message, type;
private long time;
private boolean seen;
private String from;
public Message() {
}
public Message(String message, String type, long time, boolean seen, String from) {
this.message = message;
this.type = type;
this.time = time;
this.seen = seen;
this.from = from;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public boolean isSeen() {
return seen;
}
public void setSeen(boolean seen) {
this.seen = seen;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}