Android:完成尚未停用或关闭的游标

时间:2011-07-31 09:40:35

标签: android sqlite cursor

我有一个需要使用SQLite(Android的数据库)的应用程序。

我为我的模型和datahelper创建了一个类似JPA的结构。 问题是当我在helper.search(int id)上调用另一个aHelper.search(int id)时,Finalizing a Cursor错误日志。

return Child(cursor.getInt(0), new ParentHelper.search(cursor.getInt(1)));

我的类似JPA的模型和datahelper的结构是这样的。

class Parent {
   int id;
   String name
   // constructor with field-parameter
   // getters and setters
}

class Child {
   int id;
   Parent parent;
   // constructor with field-parameter
   // getters and setters
}

class ParentHelper {
   // necessary SQLiteImpelemntations
   Parent search(int id) {
      // new Cursor implementation
      cursor.moveToFirst();
      return new Parent(cursor.getInt(0), cursor.getString(1));
   }
}

class ChildHelper {

   ParentHelper parentHelper;

   void close() {
      parenHelper.close()
      SQLiteDatabase.close();
      SQLiteOpenHelper.close();
   }

   ArrayList searchAll() {
      // new Cursor implementation
      cursor.moveToFirst();
      Child child = new Child(cursor.getInt(0), parentHelper.search(cursor.getInt(1)))
      ArrayList.add(child)
      cursor.close();
   }

   // necessary SQLiteImpelemntations
   Child search(int id) {
      // new Cursor implemenation
      cursor.moveToFirst();
      Child child = new Child(cursor.getInt(0), parentHelper.search(cursor.getInt(1)))
      cursor.close();
      return child;
   }   
}

1 个答案:

答案 0 :(得分:0)

在返回值之前尝试关闭光标。

class ParentHelper {
// necessary SQLiteImpelemntations
Parent search(int id) {
    // new Cursor implementation
    cursor.moveToFirst();
    Parent p = new Parent(cursor.getInt(0), cursor.getString(1));
    cursor.close();
    return p;
    }
}

为您的ChildHelper执行相同操作。