0
任何人都可以教我什么时候应该关闭一个打开bbdd和光标的sqlite?何时关闭bbdd并关闭光标?
我有这个类
public class DataBaseHelper
{
Context context;
private static final String DATABASE_NAME="lugaresbbdd";
private SQLiteDatabase db; // Referencia al manager.
private final int DB_VERSION = 1; // version
CustomSQLiteOpenHelper helper;
// Nombres para las tablas y campos
private final String TABLE_NAME = "lugares";
private final String TABLE_ROW_ID = "_id";
static String CNOMBRE = "nombre";
private final String CDESC = "descripcion";
private final String CLAT = "latitud";
private final String CLONG="longitud";
static String CFOTO="foto";
public DataBaseHelper(Context context)
{
this.context = context;
//Crea o abre la BBDDD
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
db = helper.getWritableDatabase();
}
public Cursor getNombres(){
//CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
//db = helper.getWritableDatabase();
Cursor respuesta = db.rawQuery("select "+TABLE_ROW_ID+","+CNOMBRE+" from "+TABLE_NAME, null);
return respuesta;
}
这:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listatab);
context = getBaseContext();
//Creamos la instancia de DataBaseHelper, un cursor y aplicamos el metodo getNombres al cursor y llamamos al metodo encargado de gestioanr ese cursor
ayudabbdd = new DataBaseHelper(this);
nombresC = (Cursor) ayudabbdd.getNombres();
startManagingCursor(nombresC);
nombresC.moveToFirst();
//Para crear un simpleCursorAdapter necesitamos
//Contexto this
//Layour donde se mostrara el resultado, generalmente un textview
//Cursor
//Cual sera el campo que recibiremos de la BBDD
//Donde tenemos que poner esa informacion, generalmente el ID correspondiente al textvies del layour del segundo parametro
String[] deNombre = new String[]{DataBaseHelper.CNOMBRE};
int[] aNombre = new int[]{R.id.nombreLugar};
lugaresNombre = new SimpleCursorAdapter(this, R.layout.entrada_lista, nombresC, deNombre, aNombre);
setListAdapter(lugaresNombre);
listaview= getListView();
listaview.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent intent = new Intent(listatab.this, mostrarLugar.class);
startManagingCursor(nombresC);
String nombreClicks= nombresC.getString(nombresC.getColumnIndex("nombre"));
intent.putExtra("nombre",nombreClicks);
startActivity(intent);
nombresC.close();
}
@Override
public void onDestroy()
{
nombresC.close();
ayudabbdd.close();
super.onDestroy();
}
@Override
protected void onPause() {
nombresC.close();
ayudabbdd.close();
super.onPause();
}
在第二类,如果我在一个项目日志猫点击我说:
android.database.sqlite。 DatabaseObjectNotClosedException:应用程序未关闭此处打开的游标或数据库对象
但是,如果我关闭onCreate方法中的游标,请不要填充列表视图,然后,当我必须关闭游标nombresC?
林编辑我的代码更容易回应我 – colymore 2012-01-06 13:53:04
我已经添加了一个更新到我的答案。 – Yury 2012-01-06 14:32:55