我正在开发一个应用程序,我在其中使用带有图像和复选框的自定义网格视图。显示带有图像和复选框的网格视图效果很好。但是这里我的问题是,如果我滚动gridview,选中一个复选框,如果我再次向下滚动,它会再次选中另一个复选框。此处复选框选中状态保持不变。我的适配器类代码是..
MyGrid.java
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
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) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.galleryitem, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
holder.textview = (TextView) convertView.findViewById(R.id.saved_image_name);
Drawable background = holder.textview.getBackground();
background.setAlpha(150);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.textview.setId(position);
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) holder.checkbox;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
selected_images.remove(String.valueOf(id));
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
if (selected_images.contains(String.valueOf(id))) {
}else{
selected_images.add(String.valueOf(id));
}
}
}
});
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);
}
});
if (arrPath[position] == null || arrPath[position].equals(null)) {
}else{
image_name=extractString(arrPath[position]);
String[] splited_name = image_name.split("\\.");
for (int i = 0; i < selected_images.size(); i++) {
if (selected_images.get(i).equals(String.valueOf(position)) || selected_images.get(i) == String.valueOf(position)) {
holder.checkbox.setChecked(true);
}
}
holder.textview.setText(splited_name[0]);
holder.imageview.setImageBitmap(thumbnails[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
holder.imageview.setScaleType(ImageView.ScaleType.FIT_XY);
}
return convertView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox,checkbox1;
TextView textview;
int id;
}
任何人都可以帮助我如何保持复选框(选中复选框)的持久性状态。