我在Android上使用此代码,但在调试时我发现此错误:
the cursor should be freed up after use with #close
此行有错误:
cursor = db.query(DATABASE_TABLECa, new String[]{ key_RoWid ,Key_GroupName}, null, null, null, null, null);
public List<ListAdapterdb> Getall() {
List<ListAdapterdb> dataList = new ArrayList<ListAdapterdb>();
db = dbhelper.getReadableDatabase();
Cursor cursor= null;
cursor = db.query(DATABASE_TABLECa, new String[]{ key_RoWid ,Key_GroupName}, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
ListAdapterdb data = new ListAdapterdb();
//data.setID(Integer.parseInt(cursor.getString(0)));
data.setItem(cursor.getString(1));
// Adding contact to list
dataList.add(data);
} while (cursor.moveToNext());
}
return dataList;
}
答案 0 :(得分:12)
完成数据处理后,您需要致电cursor.close()
。
try {
ListAdapterdb data = new ListAdapterdb();
while (cursor.moveToNext()) {
// data.setID(Integer.parseInt(cursor.getString(0)));
data.setItem(cursor.getString(1));
// Adding contact to list
dataList.add(data);
}
} finally {
cursor.close()
}