-1

I use an ArrayAdapter on listview with convertview. But when it scrolls, there are some items duplicated. Could you please help me solve the problem? thanks main code of getView function:

        View view;
        if (null != convertView) {
            view = convertView;
        } else {
            view = getActivity().getLayoutInflater().inflate(R.layout.item_fans_task, null);
            taskImg = (ImageView) view.findViewById(R.id.task_img);
            taskTitleText = (TextView) view.findViewById(R.id.task_title_text);
            taskContentText = (TextView) view.findViewById(R.id.task_content_text);
            taskProcessImg = (ImageView) view.findViewById(R.id.task_process_img);
            shareText = (TextView) view.findViewById(R.id.share_text);
            taskType = (TextView) view.findViewById(R.id.task_type);
        }
        Tasks task = getItem(position);

        taskTitleText.setText(task.getTitle());
        StringBuilder builder = new StringBuilder();
        builder.append("已将文章分享到朋友圈,并获得" + task.getReaders() +"次阅读\n");
        builder.append("已分享" + task.getTsum() + "次" + "  剩余" + task.getSynum() + "次" );
        taskContentText.setText(builder);

...

        return view;
Wang Ke
  • 455
  • 1
  • 4
  • 8

2 Answers2

1

You assign your "UI"-Variables only when there is no view to recycle (view == null). Maybe should use the ViewHolder Pattern. For the moment, the following should be ok:

    View view;
    if (null != convertView) {
        view = convertView;
    } else {
        view = getActivity().getLayoutInflater().inflate(R.layout.item_fans_task, null);
    }
    taskImg = (ImageView) view.findViewById(R.id.task_img);
    taskTitleText = (TextView) view.findViewById(R.id.task_title_text);
    taskContentText = (TextView) view.findViewById(R.id.task_content_text);
    taskProcessImg = (ImageView) view.findViewById(R.id.task_process_img);
    shareText = (TextView) view.findViewById(R.id.share_text);
    taskType = (TextView) view.findViewById(R.id.task_type);
Jörn Buitink
  • 2,906
  • 2
  • 22
  • 33
0

Your line

if (null != convertView) {
    view = convertView;
}

causes that error. Simply to fix, remove that code. If your want to your ListView Scrolling Smooth using ViewHolder. Check this tutorial for more detail http://www.vogella.com/tutorials/AndroidListView/article.html

Lan Nguyen
  • 785
  • 5
  • 13