简体   繁体   English

Android Listview在滚动时重复项

[英]Android Listview is repeating items when scrolled

I have looked at other threads and I cannot see what is wrong with my listadapter. 我查看了其他线程,但看不到listadapter出了什么问题。 I have the view holder pattern and I am setting all the values for the list item outside of the convertview null check statement. 我有视图持有者模式,并且要在convertview null检查语句之外设置列表项的所有值。 Im not sure what other things would cause it to repeat the items. 我不确定还有哪些其他原因会使它重复这些项目。

public class ScheduledJobListAdapter extends BaseAdapter {

private static LayoutInflater inflater = null;
private ArrayList<Job> jobList;
private Context context;

public ScheduledJobListAdapter(Context context, ArrayList<Job> jobList) {
    this.context = context;
    this.jobList = jobList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Job getItem(int position) { return jobList.get(position); }

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ScheduledViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item_scheduled_job,null);

        holder                  = new ScheduledViewHolder();
        holder.job              = getItem(position);
        holder.container        = convertView.findViewById(R.id.scheduled_job_layout);
        holder.routeOrder       = convertView.findViewById(R.id.route_order_textview);
        holder.location         = convertView.findViewById(R.id.location_textview);
        holder.jobRef           = convertView.findViewById(R.id.job_ref_textview);
        holder.statusIndicator  = convertView.findViewById(R.id.status_indicator);

        convertView.setTag(holder);
    } else {
        holder = (ScheduledViewHolder) convertView.getTag();
    }

    holder.routeOrder.setText(holder.job.getRouteOrder() + "");
    holder.location.setText(holder.job.getLocation());
    holder.jobRef.setText(holder.job.getJobReference());

    return convertView;
}

}

class ScheduledViewHolder {
Job job;
LinearLayout container;
TextView routeOrder;
TextView location;
TextView jobRef;
ImageView statusIndicator;
}

Here's the problem: 这是问题所在:

holder.job = getItem(position);

Remember the views may be recycled as you scroll, and the job assigned maybe used unintentionally for other positions if you assigned it that way. 请记住,滚动时视图可能会被回收,如果您以这种方式分配视图,则分配的任务可能会无意用于其他位置。 To fix it, simply assign the job after the if-else condition: 要解决此问题,只需在if-else条件之后分配作业:

if (convertView == null) {
    // ...
} else {
    // ...
}

holder.job = getItem(position); // Update the job by position
holder.routeOrder.setText(holder.job.getRouteOrder() + "");
holder.location.setText(holder.job.getLocation());
holder.jobRef.setText(holder.job.getJobReference());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM