[英]How come when a new message is received, all previous messages' timestamps are set to the same as the new one?
我的聊天輸出如下所示
在上圖中,右側聊天是,我發送給其他人的聊天消息,左側聊天是,我收到其他人的響應。現在我的問題是,當我在聊天中收到任何響應消息時,顯示該聊天的時間以及正確顯示的消息,但是當我從其他人接收到第二條響應消息時,第一次交談(接收消息)的時間將更改為第二次交談(接收消息)的時間,與上面的圖片類似。幫我。
我的聊天程序代碼如下,
public View getView(int position, View convertView, ViewGroup parent) {
ChatMessageObjects m = messagesItems.get(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (messagesItems.get(position).isSelf() == 1) {
// message belongs to you, so load the right aligned layout
convertView = mInflater.inflate(R.layout.chat_message_right,
null);
//TextView lblFrom = (TextView) convertView.findViewById(R.id.lblMsgFrom);
TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
//date and time declared on date here
TextView date = (TextView) convertView.findViewById(R.id.txtInfo);
try {
//actualDate contains date "(i.e)27-Aug-2015 6:20:25 am/pm" in this format
String actualDate = m.getDate();
Date FormatDate = new SimpleDateFormat("dd-MMM-yyyy h:mm:ss a").parse(actualDate);
//actualDate converted from "(i.e)27-Aug-2015 6:20:25 am/pm" to "6:20 pm" in this
//format for display the chat time for every chat message .
dateResult = new SimpleDateFormat("h:mm a").format(FormatDate);
// lblFrom.setText(m.getFromName());
} catch (ParseException e) {
e.printStackTrace();
}
date.setText(dateResult);
txtMsg.setText(m.getMessage());
} else {
//if (m.getMessage() != null) {
// message belongs to other person, load the left aligned layout
convertView = mInflater.inflate(R.layout.chat_message_left,
null);
//TextView lblFrom = (TextView) convertView.findViewById(R.id.lblMsgFrom);
TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
//date and time added here
final TextView date = (TextView) convertView.findViewById(R.id.txtInfo);
//Time coding start from here
String actualDate = m.getDate();
if (actualDate == null) {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String strDate = sdf.format(c.getTime());
//Time set here
date.setText(strDate);
}
m.setDate(strDate);
txtMsg.setText(m.getMessage());
}
//}
}
return convertView;
}
}
}
String actualDate = m.getDate();
if (actualDate == null) {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String strDate = sdf.format(c.getTime());
//Time set here
date.setText(strDate);
}
您在此處執行的操作是獲取m.getDate()
的值,該值為null,因為尚未設置。 然后,使用當前時間將TextView插入int。 對於新郵件,這很好,但對於較舊的郵件,則不行。
您在這里忘記要做的是將日期設置為m
。 如果在if的末尾需要m.setDate(strDate)
類的東西。
因為您當前不更改m
的日期值,所以每次嘗試使用String actualDate = m.getDate();
來獲取它時,都不會更改String actualDate = m.getDate();
,它將為null,導致日期“重置”為當前日期。
現在,當我收到消息時,查看該消息的時間。但是當我收到第二條消息時,未查看第一條消息的時間(第一條消息的時間為空)。
這是因為,當actualDate為空時,現在僅設置日期。 您可以輕松解決此問題。 它可能看起來像這樣
String actualDate = m.getDate();
if (actualDate == null) {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String strDate = sdf.format(c.getTime());
m.setDate(strDate);
}
//Time set here
date.setText(m.getDate());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.