这是我的自定义适配器的代码:
public static class ImageAdapter extends BaseAdapter {
private static Context mContext;
private static LayoutInflater mInflater;
// Keep all Images in array
private static Bitmap[] mThumbIds;
private static int mViewResourceId, pos;
private static CheckBox cb;
// Constructor
public ImageAdapter(Context ctx, int viewResourceId, Bitmap[] pics) {
mInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mThumbIds = pics;
mViewResourceId = viewResourceId;
mContext = ctx;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
// ImageButton imageView = (ImageButton)
// convertView.findViewById(R.id.icon);
cb = new CheckBox(mContext);
Drawable background = new BitmapDrawable(mThumbIds[position]);
cb.setBackgroundDrawable(background);
pos = position;
System.out.println("Setting checkbox set: "+imageIsDup[pos]);
cb.setChecked(imageIsDup[pos]);
System.out.println("Has checkbox been set? "+cb.isChecked());
cb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (cb.isChecked()) {
imageIsDup[pos] = true;
} else
imageIsDup[pos] = false;
}
});
return cb;
}
}
}
我正在这样设置适配器:
GridView list = (GridView) dialog
.findViewById(R.id.grid_view);
TextView no = (TextView) dialog
.findViewById(R.id.noOfDups);
no.setText("Found " + noOfImages
+ " duplicates. Please verify.");
//list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
dialog.setTitle("Images Found");
dialog.setCancelable(true);
// ImageAndTextAdapter adapter = new ImageAndTextAdapter
// (longOperationContext, R.layout.row, imageNames,
// imageLocs, thumb);
ImageAdapter adapter = new ImageAdapter(
longOperationContext, R.layout.row, thumb);
System.out.println("No of images:"+thumb.length);
list.setAdapter(adapter);
如果我单击并取消单击它们,这些复选框可以正常工作。不起作用的是setChecked()
功能。虽然参数是true
显示时没有设置复选框。错误是什么?
这段代码以前可以工作,不久前我做了一些编辑,但从那以后就没有工作了。遗憾的是我不记得编辑了。
更新
即使只有 8 张图片,而且println
消息应该16
只有48
. 第一组只是假的,后两组具有正确的imageIsDup
值。