我有以下代码的问题:
当我用我的对象列表实例化适配器时,列表的顺序仍然很好,前七个可见行也是如此。但是当我向下滚动列表并且convertView
被回收时,我会得到一些重复的标题/副标题(例如,第八行有第二行的标题,第四行的第九行,总是相同的模式)。由于某些可见的行具有相同的标题/副标题,因此该错误不能用于回收视图。
public RomantikObjectAdapter(Context context, List<RomantikObject> items) {
super(context, R.layout.listitem_object, items);
inflater = LayoutInflater.from(context);
mContext = context;
}
static class ViewHolder {
TextView title;
TextView subtitle;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listitem_object, parent, false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.subtitle = (TextView) convertView.findViewById(R.id.subtitle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
RomantikObject item = getItem(position);
holder.title.setText(item.getTitle());
holder.subtitle.setText(item.getSubtitle());
return convertView;
}
除getItem(position)
外,一切似乎都运行正常。我尝试在课程中的某个字段中添加items
,因此我可以items.get(position)
。但我得到了完全相同的错误订单。
也许这份名单是在我不知情的情况下重新订购的?我怎样才能发现或避免这种情况?
答案 0 :(得分:0)
复制此代码并运行
List<RomantikObject> thisArray;
String value1,value2;
public RomantikObjectAdapter(Context context, List<RomantikObject> items) {
super(context, R.layout.listitem_object, items);
inflater = LayoutInflater.from(context);
mContext = context;
this.thisArray=item;
}
static class ViewHolder {
TextView title;
TextView subtitle;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listitem_object, parent, false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.subtitle = (TextView) convertView.findViewById(R.id.subtitle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
value1 = items.get(position).getTitle();
value2 = items.get(position).getSubtitle();
holder.title.setText(value1);
holder.subtitle.setText(value2);
return convertView;
}