Android,查看Holder无法正确更新listview

时间:2015-02-06 00:28:54

标签: android listview android-listview android-viewholder

我正在从数据库中将数据读入我的列表视图中,我可以在列表视图中看到信息,每行中的数据分为左右两侧。当我在列表视图中滚动时,项目不正确。列表视图右侧的一些项目向左侧移动,反之亦然。 我可以通过直接在getView()下设置convertView = null来解决这个问题,这是一个非常糟糕的解决方案!如何妥善解决问题?

代码链接:

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 个答案:

答案 0 :(得分:1)

试试这个。

getViewTypeCount()应返回2,因为您有两种类型的布局。