我的聊天输出在下面,

在上面的图片中,右侧聊天的是,我被发送给别人的聊天消息和左边聊天的是,我收到来自others.Now的响应我的问题是当我收到任何响应消息时,聊天的时间和显示的时间一样,但是当我收到别人的第二次响应消息时,第一次聊天(接收消息)的时间被更改为第二次聊天(接收消息)时间,类似于我在image.How上解释了解决这个问题的时间--任何人帮助我。
我的聊天程序代码如下,
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;
}
}
}发布于 2015-09-03 06:25:59
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()的值,它是空的,因为它还没有设置。然后,使用当前的时间来插入TextView。对于新消息来说,这是可以的,但对于较旧的消息,则不是这样。
您在这里忘记做的是设置m的日期。在if的末尾需要类似m.setDate(strDate)的东西。
因为您目前没有更改m的日期值,所以每次尝试使用String actualDate = m.getDate();获取它时,它都是空的,从而导致将日期“重置”到当前日期。
现在,当我收到消息时,该消息的时间,viewed.But,当我收到第二条消息时,第一条消息的时间不会被查看(第一条消息的时间是空的)。
这是因为现在只在actualDate为null时设置日期。你可以很容易地解决这个问题。它可能看起来像这样
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());https://stackoverflow.com/questions/32367903
复制相似问题