在我的应用程序中,我有一个在gridView中显示图像的活动。所以我使用了一个扩展BaseAdapter的Adapter类。在该适配器类中,我打开了一个游标并从getView()方法返回图像。
所以现在我的问题是,当我得到异常时,我应该在哪里关闭光标。我不能使用startManagingCursor(),因为它已被弃用。任何其他解决方案将不胜感激。
我的onCreate方法
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.splitted_grid);
System.out.println("started new splittedimageactivity");
//splittedBitmaps = getIntent().getParcelableArrayListExtra("splitted_images");
int size = getIntent().getIntExtra("image_numbers", 0);
int width = getIntent().getIntExtra("width", 0);
int height = getIntent().getIntExtra("height", 0);
gridView = (GridView) findViewById(R.id.image_grid_view);
SplittedImageAdapter s = new SplittedImageAdapter(this, size, width, height);
gridView.setAdapter(s);
gridView.setNumColumns((int) Math.sqrt(size));//splittedBitmaps.size()));
s.close();// This causes exception trying to re-open already closed cursor. If I remove this, then exception is your cursor is not closed, call close() explicitly
}
我的适配器类
public class SplittedImageAdapter extends BaseAdapter{
private Context mContext;
//private List<Bitmap> mSplittedBitmaps;
public ViewGroup mParentView = null;
private int noOfImages, imageWidth, imageHeight;
private Cursor mCursor;
public SplittedImageAdapter(Context c, int size, int w, int h){
mContext = c;
noOfImages = size;
imageWidth = w;
imageHeight = h;
DBAdapter db = new DBAdapter(c);
db.open();
mCursor = db.getAllImages();
System.out.println(mCursor+"cursor opened");
}
@Override
public int getCount() {
return noOfImages;//mSplittedBitmaps.size();
}
@Override
public Object getItem(int position) {
return position;//mSplittedBitmaps.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mParentView = parent;
ImageCell imageView = null;
if(convertView == null){
imageView = new ImageCell(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth-4, imageHeight-4));//65,65));//SplittedImageActivity.splittedBitmaps.get(position).getWidth()-4, SplittedImageActivity.splittedBitmaps.get(position).getHeight()-4));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
}else{
imageView = (ImageCell) convertView;
}
imageView.mCellNumber = position;
imageView.mGrid = (GridView) mParentView;
imageView.setId(position);
if(position == noOfImages-1){//SplittedImageActivity.splittedBitmaps.size()-1 == position){
imageView.mEmpty = true;
imageView.setBackgroundResource(R.color.cell_empty);
}
else{
//System.out.println(mCursor.getCount());
mCursor.moveToPosition(position);
Bitmap b = BitmapFactory.decodeFile(mCursor.getString(4));//"/sdcard/Android/data/com.softtrends.puzzle/files/Pictures/image"+position+".jpg");
imageView.setImageBitmap(b);//SplittedImageActivity.splittedBitmaps.get(position));
imageView.mEmpty = false;
}
imageView.setOnClickListener((OnClickListener) mContext);
imageView.setOnLongClickListener((OnLongClickListener) mContext);
return imageView;
}
public void close(){
mCursor.close();
}
}
答案 0 :(得分:0)
这种情况正在发生,因为您并不是专门调用close()方法。
在适配器的getView()方法中返回imageView之前执行此操作并删除s.close()
,因为s
是适配器而不是游标。