I am facing a problem. I am working on QuickBlox Private chat. I want to show date and time with only last message of sent and get. I achieve that but issue is that when ever I scroll that message listview date and time change position and every scroll it shows with other messages not with last. How can I fix that? My BaseAdapter code is below.
public class ChatAdapter extends BaseAdapter {
private final List<QBChatMessage> chatMessages;
private Activity context;
private static String inComingMessage = "0";
private static String outGoingMessage = "0";
public ChatAdapter(Activity context, List<QBChatMessage> chatMessages,
String inComingMessage, String outGoingMessage) {
this.context = context;
this.chatMessages = chatMessages;
this.inComingMessage = inComingMessage;
this.outGoingMessage = outGoingMessage;
}
@Override
public int getCount() {
if (chatMessages != null) {
return chatMessages.size();
} else {
return 0;
}
}
@Override
public QBChatMessage getItem(int position) {
if (chatMessages != null) {
return chatMessages.get(position);
} else {
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
QBChatMessage chatMessage = getItem(position);
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = vi.inflate(R.layout.list_item_message, null);
holder = createViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
QBUser currentUser = ((ApplicationSingleton) context.getApplication())
.getCurrentUser();
boolean isOutgoing = chatMessage.getSenderId() == null
|| chatMessage.getSenderId().equals(currentUser.getId());
setAlignment(holder, isOutgoing);
holder.txtMessage.setText(chatMessage.getBody());
if (chatMessage.getSenderId() != null) {
if (chatMessage.getSenderId().equals(currentUser.getId())) {
if (outGoingMessage.equals("1")) {
if (chatMessage.isRead()) {
holder.txtDateTime
.setText("Seen: "
+ convertDate(chatMessage.getDateSent() * 1000));
} else {
holder.txtDateTime.setText(convertDate(chatMessage
.getDateSent() * 1000));
}
outGoingMessage = "0";
}
} else {
if (inComingMessage.equals("1")) {
holder.txtDateTime.setText("Seen: "
+ convertDate(chatMessage.getDateSent() * 1000));
inComingMessage = "0";
}
}
} else {
holder.txtDateTime
.setText(convertDate(chatMessage.getDateSent() * 1000));
}
return convertView;
}
public void add(QBChatMessage message) {
chatMessages.add(message);
}
public void add(List<QBChatMessage> messages) {
chatMessages.addAll(messages);
}
private void setAlignment(ViewHolder holder, boolean isOutgoing) {
if (!isOutgoing) {
holder.contentWithBG
.setBackgroundColor(Color.parseColor("#00aeef"));
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG
.getLayoutParams();
layoutParams.gravity = Gravity.RIGHT;
holder.contentWithBG.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content
.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.content.setLayoutParams(lp);
layoutParams = (LinearLayout.LayoutParams) holder.txtMessage
.getLayoutParams();
layoutParams.setMargins(0, 10, 20, 0);
layoutParams.gravity = Gravity.RIGHT;
holder.txtMessage.setLayoutParams(layoutParams);
layoutParams = (LinearLayout.LayoutParams) holder.txtDateTime
.getLayoutParams();
layoutParams.gravity = Gravity.RIGHT;
holder.txtDateTime.setLayoutParams(layoutParams);
} else {
holder.contentWithBG
.setBackgroundColor(Color.parseColor("#F5F5F5"));
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG
.getLayoutParams();
layoutParams.gravity = Gravity.LEFT;
holder.contentWithBG.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content
.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.content.setLayoutParams(lp);
layoutParams = (LinearLayout.LayoutParams) holder.txtMessage
.getLayoutParams();
layoutParams.setMargins(0, 10, 20, 0);
layoutParams.gravity = Gravity.LEFT;
holder.txtMessage.setLayoutParams(layoutParams);
layoutParams = (LinearLayout.LayoutParams) holder.txtDateTime
.getLayoutParams();
layoutParams.gravity = Gravity.LEFT;
holder.txtDateTime.setLayoutParams(layoutParams);
}
}
private ViewHolder createViewHolder(View v) {
ViewHolder holder = new ViewHolder();
holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);
holder.content = (LinearLayout) v.findViewById(R.id.content);
holder.contentWithBG = (LinearLayout) v
.findViewById(R.id.contentWithBackground);
holder.txtDateTime = (TextView) v.findViewById(R.id.txtDateTime);
return holder;
}
public static String convertDate(long dateInMilliseconds) {
return DateFormat.format("hh:mm", (dateInMilliseconds)).toString();
}
private static class ViewHolder {
public TextView txtMessage;
public TextView txtDateTime;
public LinearLayout content;
public LinearLayout contentWithBG;
}
}
Whenever loading the listview you need to consider all the scenario. What i mean was if you are setting text for textview using if condition you must write else condition and need to write action for that textview. If you don't consider else part then you got that issue. When you scroll data is changed.
In your case you are using the following code
if (inComingMessage.equals("1")) {
holder.txtDateTime.setText("Seen: "
+ convertDate(chatMessage.getDateSent() * 1000));
inComingMessage = "0";
}
But there is no else part here. So when this if condition fails the data will change. So write your else part.
This is one scenario. Try this.