Reputation: 147
My chat output is looking below,
In above image,The right side chat's are,Chat message I was send to others and left side chat's are, I am receive response from others.Now my issue is when i am receive any response message while chat the time of that chat is displayed along with that message displayed properly,But when i am receiving second response message from others the time of first chat(receive message) is changed as second chat(receive message) time similarly that i am explained above image.How to fix this issue anyone help me.
My programming code for chat is below,
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;
}
}
}
Upvotes: 1
Views: 129
Reputation: 43354
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);
}
What you do here is you get the value of m.getDate()
which is null because it is not yet set. Then you use the current time to put int the TextView. That is fine for new messages, but not for messages that are older.
What you forgot to do here, is set the date of m
. You need something like m.setDate(strDate)
at the end of the if.
Because you currently don't change m
's date value, everytime you try to get it with String actualDate = m.getDate();
, it will be null, causing the date to be 'reset' to the current date.
Now when i am receive the message the time for that message viewed.But when i am receive the second message the time of first message doesn't viewed(time of first message is empty).
This is because now only the date is set when the actualDate is null. You can solve this easily. It could look like this
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());
Upvotes: 1