我已经尝试了几天寻找这个问题的解决方案,但我很难过。
这是我到目前为止所拥有的:
自定义 BaseAdapter 类:
public static class ImageAdapter extends BaseAdapter {
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;
}
@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, list, false);
cb = (CheckBox) convertView.findViewById(R.id.select);
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 convertView;
}
}
设置gridView的代码:
final Dialog dialog = new Dialog(longOperationContext);
dialog.setContentView(R.layout.activity_list);
TextView no = (TextView) dialog
.findViewById(R.id.noOfDups);
no.setText("Found " + noOfImages
+ " duplicates. Please verify.");
dialog.setTitle("Images Found");
dialog.setCancelable(false);
list = (GridView) dialog
.findViewById(R.id.grid_view);
ImageAdapter empty=new ImageAdapter(longOperationContext, R.layout.row, new Bitmap[0]);
imageAdapter = new ImageAdapter(
longOperationContext, R.layout.row, thumb);
dialog.show();
imageAdapter.notifyDataSetChanged();
list.invalidateViews();
list.setAdapter(empty);
list.setEmptyView(new View(longOperationContext));
list.invalidateViews();
list.setAdapter(imageAdapter);
我假设此代码会gridView
在开始时将 设置为空视图,然后设置为适配器的内容。
我从文档中读到,这些removeView
函数在抛出Unsupported Exception
. 如何清除网格视图的先前内容(如果有)并设置新内容?