何时关闭 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?
anyone can teach me when i should to close a sqlite open bbdd and a cursor?
I have this class
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;
}
And this:
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();
}
In the second class if i click on a item log cat said me:
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
But if i close the cursor in onCreate method, don't populate me the list view, then, when i must to close the cursor nombresC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太清楚,但就我而言,我认为您必须在活动的 onResume 方法中打开数据库并在 onPause 上关闭它。至于游标,您应该在不需要它们后将其关闭。例如,如果您的数据库帮助程序类作为查询结果返回游标,那么您应该在处理游标的所有行时关闭它。
我建议您考虑记事本示例。有一些关于如何在 Android 中使用数据库的非常好的技巧。
更新:这是我通常如何在活动中填充列表的示例:
这是我的帮助器类:
您可以将其用作案例的模板。
I don't know precisely, but for my opinion I think that you have to open database in onResume method of your activity and close it onPause. As for Cursors you should close them after you do not need them. For instance, if your database helper class returns cursor as a result of query then you should close it when you process all rows of the cursor.
I advice you to consider Notepad example. There are some very good tips how to work with databases in Android.
UPDATE: Here is an example how I usually populate my list in an activity:
And this is my helper class:
You can use it as a template for your case.