android如何在这样的类中关闭游标

android如何在这样的类中关闭游标,android,android-sqlite,android-cursor,Android,Android Sqlite,Android Cursor,我的dbhelper类中有以下函数: public Cursor getRow(long rowId) { String where = KEY_ROWID + "=" + rowId; Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null); if (c != null) { c.moveToFirst(); } return c

我的dbhelper类中有以下函数:

public Cursor getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
    if (c != null) {
        c.moveToFirst();
    }

    return c;
}
当我来到这个类调用的活动时,我得到了这个错误(请注意,当我进入这个活动时,我得到了这个错误,而不是离开这个活动时):

这是一个很长的错误

有人告诉我使用c.close();在返回值c上方,类似以下内容:

public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
        if (c != null) {
            c.moveToFirst();
        }
        c.close();
        return c;
    }  
我尝试过,但现在出现了以下错误:

Access closed cursor

我该怎么办?如何关闭该类中的游标

您应该关闭
活动中的
光标
。像

 Cursor c=getRow(rowID_Value);
 c.close();

使用
DB
后,您必须关闭此
光标

您应该关闭
活动中的
光标
。像

 Cursor c=getRow(rowID_Value);
 c.close();

使用
DB
后,您必须关闭此
光标

您应该关闭
活动中的
光标
。像

 Cursor c=getRow(rowID_Value);
 c.close();

使用
DB
后,您必须关闭此
光标

您应该关闭
活动中的
光标
。像

 Cursor c=getRow(rowID_Value);
 c.close();

使用
DB
后,必须关闭此
光标

使用
getRow
功能后关闭光标,即

public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    } 
像这样使用它

Cursor c1 = getRow(rowId);
//use cursor 
//....
if(c1 != null)c1.close();
注意:rowId是要从数据库中以长格式显示的行的值

编辑

创建
光标c1
首先检查游标是否不为null,然后关闭它

if(c1 != null)c1.close(); 
然后将光标设为

 c1 = getRow(rowId);
和onStop/onDestroy关闭光标作为

if(c1 != null)c1.close();

使用
getRow
函数后关闭光标,即

public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    } 
像这样使用它

Cursor c1 = getRow(rowId);
//use cursor 
//....
if(c1 != null)c1.close();
注意:rowId是要从数据库中以长格式显示的行的值

编辑

创建
光标c1
首先检查游标是否不为null,然后关闭它

if(c1 != null)c1.close(); 
然后将光标设为

 c1 = getRow(rowId);
和onStop/onDestroy关闭光标作为

if(c1 != null)c1.close();

使用
getRow
函数后关闭光标,即

public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    } 
像这样使用它

Cursor c1 = getRow(rowId);
//use cursor 
//....
if(c1 != null)c1.close();
注意:rowId是要从数据库中以长格式显示的行的值

编辑

创建
光标c1
首先检查游标是否不为null,然后关闭它

if(c1 != null)c1.close(); 
然后将光标设为

 c1 = getRow(rowId);
和onStop/onDestroy关闭光标作为

if(c1 != null)c1.close();

使用
getRow
函数后关闭光标,即

public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    } 
像这样使用它

Cursor c1 = getRow(rowId);
//use cursor 
//....
if(c1 != null)c1.close();
注意:rowId是要从数据库中以长格式显示的行的值

编辑

创建
光标c1
首先检查游标是否不为null,然后关闭它

if(c1 != null)c1.close(); 
然后将光标设为

 c1 = getRow(rowId);
和onStop/onDestroy关闭光标作为

if(c1 != null)c1.close();

你好,我面临着同样的问题,我解决了这个问题,就像

当我查询并返回
ArrayList
JSON对象时
不是
cursor

您是否需要更改您的查询

public JSONObject getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
   if (c!= null & c.moveToFirst()) {
        JSONObject jsonObj = new JSONObject();

         jsonObj.put("key_Name", c.getString(c.getColumnIndex("your_column_name")));
         // do for your all column and return this jsonObj and before returning close cursor
    }
    c.close();
    return jsonObj;
}  
这只用于返回一条记录,以便重新调整我正在使用的记录集


它将自行管理游标操作并返回给我们
sqliteCursorLoader

您好,我遇到了同样的问题,我解决了这个问题

当我查询并返回
ArrayList
JSON对象时
不是
cursor

您是否需要更改您的查询

public JSONObject getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
   if (c!= null & c.moveToFirst()) {
        JSONObject jsonObj = new JSONObject();

         jsonObj.put("key_Name", c.getString(c.getColumnIndex("your_column_name")));
         // do for your all column and return this jsonObj and before returning close cursor
    }
    c.close();
    return jsonObj;
}  
这只用于返回一条记录,以便重新调整我正在使用的记录集


它将自行管理游标操作并返回给我们
sqliteCursorLoader

您好,我遇到了同样的问题,我解决了这个问题

当我查询并返回
ArrayList
JSON对象时
不是
cursor

您是否需要更改您的查询

public JSONObject getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
   if (c!= null & c.moveToFirst()) {
        JSONObject jsonObj = new JSONObject();

         jsonObj.put("key_Name", c.getString(c.getColumnIndex("your_column_name")));
         // do for your all column and return this jsonObj and before returning close cursor
    }
    c.close();
    return jsonObj;
}  
这只用于返回一条记录,以便重新调整我正在使用的记录集


它将自行管理游标操作并返回给我们
sqliteCursorLoader

您好,我遇到了同样的问题,我解决了这个问题

当我查询并返回
ArrayList
JSON对象时
不是
cursor

您是否需要更改您的查询

public JSONObject getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, where, null, null, null,null, null);
   if (c!= null & c.moveToFirst()) {
        JSONObject jsonObj = new JSONObject();

         jsonObj.put("key_Name", c.getString(c.getColumnIndex("your_column_name")));
         // do for your all column and return this jsonObj and before returning close cursor
    }
    c.close();
    return jsonObj;
}  
这只用于返回一条记录,以便重新调整我正在使用的记录集



它将管理游标操作本身,并返回我们
sqliteCursorLoader

在迭代后关闭游标。您将返回一个关闭的游标。拆下c.关闭();您应该在(游标迭代)类中关闭游标,该类可能是主活动或其他内容。@Lavkushagrawal感谢您的回复,如果我正确理解您的意思,您的意思是我关闭游标在顶部()或onDestroy(),我正在这样做,但这并不能解决我的问题,当我进入活动时,我得到了这个错误,我仍在活动中,没有离开它。不要在onStop/Destroy中关闭光标,应该在完成对光标的迭代后立即释放光标。迭代后关闭光标。您将返回一个关闭的光标。拆下c.关闭();您应该在(游标迭代)类中关闭游标,该类可能是主活动或其他内容。@Lavkushagrawal感谢您的回复,如果我正确理解您的意思,您的意思是我关闭游标在顶部()或onDestroy(),我正在这样做,但这并不能解决我的问题,当我进入活动时,我得到了这个错误,我仍在活动中,没有离开它。不要在onStop/Destroy中关闭光标,应该在完成对光标的迭代后立即释放光标。迭代后关闭光标。您将返回一个关闭的光标。拆下c.关闭();您应该在(游标迭代)类中关闭游标,该类可能是主要活动或其他内容。@lavkushagrawal感谢您的回复,如果我正确理解您的意思,您的意思是我关闭游标在顶部()或onDestroy(),我正在这样做,但这并不能解决我的问题,当我