Android的,鉴于持有人不能正确地更新列表视图持有人、视图、正确地、列表

2023-09-07 00:55:07 作者:豬頭↘

我从数据库中读取数据到我的列表视图,我可以看到在列表视图中的信息,每一行中的数据被分为左,右。当我在滚动的物品不正确uppdating列表视图。在右侧关闭列表视图一些项目被移动到左侧和周围的其他方式。我可以让这个问题由直属getView(),这是一个很糟糕的解决方案设置convertView = NULL走开!如何妥善解决这个问题?

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链接:

XML http://pastebin.com/rCbeBS6E

ChatFragment http://pastebin.com/xMFCm62s

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;
}

}

推荐答案

试试这个。

getViewTypeCount()应该返回2,因为你有2个类型的布局。

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