如何/在何处从自定义列表适配器中关闭数据库连接?
我必须从自定义列表适配器中执行数据库查找。这意味着我必须在适配器中打开数据库。当我将原始活动更改为另一个时,我收到“DatabaseObjectNotClosedException”。我尝试了所有生命周期事件,并且没有在自定义适配器中调用。
onDestroy / onStop,其他所有内容都不会被调用。
一些代码
public class myAdapter extends SimpleCursorAdapter
{
private db mDbHelper;
private Cursor c;
private Context context;
public myAdapter(Context context, int layout,
Cursor c, String[] from, int[] to )
{
super(context, layout, c, from, to );
this.c = c;
this.context = context;
mDbHelper = new db ( context );
mDbHelper.open();
}
public View getView(final int pos, View inView, ViewGroup parent) {
View v = inView;
if ( v == null )
{
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.my_row, parent, false);
}
this.c.moveToPosition(pos);
String n = this.c.getString( this.c.getColumnIndex("name" ));
String c = this.c.getString( this.c.getColumnIndex("cat" ));
int vCAT_COLOR[] = mDbHelper.dbio_getCatColors( loc_cat );
// dispay data here in correct color
}
public void onDestroy() { Log.w("TAG", "onDestroy" ); mDbHelper.close(); }
public void onStop()() { Log.w("TAG", "onStop" ); mDbHelper.close(); }
}