Android, view Holder does not update listview correctly

蓝咒 提交于 2019-12-10 11:54:35

问题


I'm reading data into my listview from a database and i can see the information in the listview, the data in each row is split into left and right. When i scroll in the listview the items are not uppdating correctly. Some items in the right side off the listview are moving to the left side and the other way around. I can make this problem go away by setting convertView = null directly under the getView() which is a very bad solution! How to solve the problem properly?

Code links:

Xml "http://pastebin.com/rCbeBS6E"

ChatFragment "http://pastebin.com/xMFCm62s"

public class ChatAdapter extends ArrayAdapter<Message> {

private ArrayList<Message> lstMessages;
private LayoutInflater layoutInflater;

public ChatAdapter(Context context, ArrayList<Message> messages) {
    super(context, 0, messages);
    this.lstMessages = messages;
    layoutInflater = LayoutInflater.from(getContext());
}

public int IsMsgFromMe(Message message) {
    boolean isSenderMe = ChatFragment.username.equals(message.GetFrom());
    if (isSenderMe) {
        return 0;
    }
    return 1;
}

@Override
public Message getItem(int position) {
    return lstMessages.get(position);
}

@Override
public int getCount() {
    return lstMessages.size();
}

//Number of layouts
@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public int getItemViewType(int position) {
    Message message = lstMessages.get(position);
    int sender = IsMsgFromMe(message);
    return sender;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    //Warning very bas solution!!!!
    convertView = null; //Make it possible to scroll without loading data over an already existing view.
    int sender = getItemViewType(position);

    if (convertView == null) {
        holder = new ViewHolder();

        //Check who has sent the message me or someone else...
        switch(sender) {
            case 0:
                convertView = layoutInflater.inflate(R.layout.row_chat_me, parent, false);

                holder.chatFrom = (TextView) convertView.findViewById(R.id.txt_message_me_from);
                holder.chatMessage = (TextView) convertView.findViewById(R.id.txt_message_me_message);
                holder.chatTime = (TextView) convertView.findViewById(R.id.txt_message_me_time);
                break;
            case 1:
                convertView = layoutInflater.inflate(R.layout.row_chat_others, parent, false);

                holder.chatFrom = (TextView) convertView.findViewById(R.id.txt_message_others_from);
                holder.chatMessage = (TextView) convertView.findViewById(R.id.txt_message_others_message);
                holder.chatTime = (TextView) convertView.findViewById(R.id.txt_message_others_time);
                break;
        }
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Get the data item for this position
    Message message = lstMessages.get(position);

    // Populate the data into the template view using the data object
    holder.chatFrom.setText("From: " + message.GetFrom());
    holder.chatMessage.setText(message.GetMsg());
    holder.chatTime.setText("Date: " + message.GetTime());

    // Return the completed view to render on screen
    return convertView;
}

public static class ViewHolder {
    public TextView chatFrom;
    public TextView chatMessage;
    public TextView chatTime;
}

}


回答1:


Try this.

getViewTypeCount() should be returning 2 since you have 2 types of layouts.



来源:https://stackoverflow.com/questions/28356606/android-view-holder-does-not-update-listview-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!