显示的消息时间与所有接收消息类似

时间:2015-09-02 05:58:16

标签: java android

当一个发送消息和接收消息时,聊天输出看起来像这样(右侧是向其他人发送消息而左侧是从其他人接收消息)。 enter image description here

当我收到来自其他人的第二条消息时,上一次接收消息的时间被更改为当前接收消息时间。在我的第一个聊天窗口中,接收消息时间是“晚上11:02”但是,在我的第二个图像中,上一个接收消息时间被改变为当前时间(即)“晚上11点05分”。如何解决这个问题,请帮助我。

enter image description here

我的编程代码在

之下
   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{
                        // message belongs to other person, load the left aligned layout

                        convertView = mInflater.inflate(R.layout.chat_message_left,
                                null);

                        TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
                        TextView date = (TextView) convertView.findViewById(R.id.txtInfo);
                        String actualDate = m.getDate();           

                       if(actualDate==null)
                       {
                        Calendar c = Calendar.getInstance();
                        SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
                        String strDate = sdf.format(c.getTime());
                        date.setText(strDate);
                       }
                        txtMsg.setText(m.getMessage());

}

        }

        return convertView;
    }

1 个答案:

答案 0 :(得分:0)

else块似乎没有使用m.getDate()。遵循与if块相同的程序,即 (假设m = messageItems.get(position)

String actualDate = m.getDate();
Date FormatDate = new SimpleDateFormat("dd-MMM-yyyy h:mm:ss a").parse(actualDate);
dateResult = new SimpleDateFormat("h:mm a").format(FormatDate);