android - 使用#close 后应释放游标

标签 android

我正在为 Android 使用此代码,但在调试时我看到此错误: 使用#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;

    }

最佳答案

处理完数据后,您需要调用 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()
}

关于android - 使用#close 后应释放游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34109463/

相关文章:

android - 在 AlertDialog.Builder 中使用 findViewById() 时出现 NullPointerException

php - 尽管有一些被裁剪,如何识别相似的图像?

android - Android 4.4.+ 上的 db4o : storing an object fails because cannot cast long to integer

android - Google Place Picker 获取城市?

android - Gradle同步失败:原因:https://downloads.gradle.org/distributions/gradle-2.3.2-all.zip

android - 冒号编码(:) with Google Cloud Endpoints, Android Studio

Java:将包含天、小时、分钟的字符串转换为日期对象

php - 如何使用Volley通过JSON从mysql获取多行数据

java - 在android中编辑动态创建的按钮

java - 从 tabhost 中删除选项卡会导致应用程序崩溃