android-ViewHolder ImageView setVisibility重复
作者:互联网
我有一个带有ViewHolder模式的适配器.它具有一个TextView和一个ImageView.我必须对适配器中的数据库进行一些检查,以显示或不显示ImageView.
问题是将View.VISIBLE的可见性也设置为其他位置的ImageView.我确定我的数据库不包含那些与那些imageview相关的数据,而只包含那些正确设置的数据.
这是我的代码(我删除了textview集合的一部分,因为它运作良好):
public class ChaptersAdapter extends ArrayAdapter<List> {
private final Context context;
private final ArrayList<List> list;
ViewHolder viewHolder;
DatabaseHandler dh;
SQLiteDatabase db;
ArrayList<BookmarksHandler> bookmarksAL;
public ChaptersAdapter(Context context, ArrayList<List> list) {
super(context, R.layout.chaptersadapter, list);
this.context = context;
this.list = list;
}
static class ViewHolder{
TextView titolo;
ImageView imageView;
}
@Override
public View getView(int position, View rowView, ViewGroup parent) {
dh = new DatabaseHandler(context);
db = dh.getWritableDatabase();
bookmarksAL = dh.getAllBookmarks(db);
List chapters = getItem(position);
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.chaptersadapter, parent, false);
viewHolder = new ViewHolder();
viewHolder.titolo = (TextView) rowView.findViewById(R.id.textView);
viewHolder.imageView = (ImageView) rowView.findViewById(R.id.imageView);
rowView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) rowView.getTag();
}
for(int i = 0; i<bookmarksAL.size(); i++){
if(bookmarksAL.get(i).getId_chapter().equals((String)chapters.get(3))){
viewHolder.imageView.setVisibility(View.VISIBLE);
}
}
return rowView;
}
}
这是我的意思的屏幕:
只需要添加带圆圈的imageview,而不是上面带有X的imageview.如果我向下滚动列表视图,则视图持有者会将可见性设置为对不在数据库中的其他图像视图也可见.
解决方法:
由于其回收机制,这是ListView的正常行为.如果不符合条件,请重置ImageView的可见性
for(int i = 0; i<bookmarksAL.size(); i++){
if(bookmarksAL.get(i).getId_chapter().equals((String)chapters.get(3))) {
viewHolder.imageView.setVisibility(View.VISIBLE);
break;
} else {
viewHolder.imageView.setVisibility(View.GONE);
}
}
标签:android-viewholder,listview,adapter,android 来源: https://codeday.me/bug/20191028/1953401.html