我创建了一个网格视图,其中包含图像视图和复选框,并在该网格视图中扩展了我的复选框和图像视图。如果我检查了我的复选框,然后向下/向上滚动网格视图,则已选中网格的复选框已被取消选中。 我的代码有什么问题
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context;
public ImageAdapter(Context c, boolean checkAll) {
context = c;
isCheckAll = checkAll;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.a_6th_main_checkbox, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.img_thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.cbo_CheckImage);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if(cb.isChecked()){
cb.setChecked(true);
thumbnailsselection[id] = true;
}
else if (!cb.isChecked()){
cb.setChecked(false);
thumbnailsselection[id] = false;
}
}
});
final int id = holder.checkbox.getId();
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
CheckBox check = (CheckBox) buttonView;
if(buttonView.isChecked() == isChecked){
thumbnailsselection[id] = true;
}
else {
thumbnailsselection[id] = false;
}
}
});
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
startActivity(intent);
}
});
holder.imageview.setImageBitmap(thumbnails[position]);
if (!isCheckAll){
holder.checkbox.setChecked(false);
}else{
holder.checkbox.setChecked(true);
}
holder.id = position;
return convertView;
}
}
class ViewHolder {
CheckBox checkbox, cbo_SelectAll;
ImageView imageview;
int id;
}
}