【发布时间】:2017-06-28 03:00:25
【问题描述】:
我有以下代码:
public Cursor getListText(String [] cols) {
Cursor cursor = null;
if(cols != null && cols.length > 0) {
openDb(false);
cursor = db.query(true, Contract.Text.TABLE_NAME, cols, null, null, null
, null, null, null);
if(!cursor.moveToFirst()) {
cursor = null;
}
closeDb();
}
return cursor;
}
getListText(String [] cols) 返回的光标被SimpleCursorAdapter 使用以显示ListView。
如果我不调用cursor.moveToFirst() Android 会抛出异常:
java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
为什么会发生这种情况?这是事实,只有当我关闭数据库closeDb();时才会发生这种情况@
如果有必要,我附上SimpleCursorAdapter 实现:
private void listViewSetup() {
this.listView = (ListView) findViewById(R.id.texts);
CursorAdapterDto cursorAdapterDto = this.presenter.getListTextNames();
Cursor cursor = cursorAdapterDto.getCursor();
Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(cursor));
String [] contractNames = cursorAdapterDto.getContractNames();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.custom_list_item, cursor,
new String [] {"title"}, new int[] {R.id.itemTextView} , 2);
listView.setAdapter(adapter);
if(cursor == null) {
Toast.makeText(this, "No hay textos",
Toast.LENGTH_LONG).show();
}
}
感谢您的帮助!
已编辑 对不起!我想我没有解释这个问题!我知道我必须在关闭数据库之前管理游标。我想知道的是 SimpleCursorAdapter 如何使用光标。我的意思是如果我移动到第一个光标并关闭数据库 SimpleCursorAdapter 绘制 listView 项目! 但是如果我不移动到第一个并关闭数据库,为什么我无法访问这些项目?为什么光标内容不可用?
【问题讨论】:
标签: android sqlite simplecursoradapter