user3718930
user3718930

Reputation: 381

android-how to close cursor in a class like this

I have this function inside my dbhelper class :

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;
}

when I come to the activity where this class calls, I get this error (Please notice when I enter this activity I get this error not when leaving the activity ):

Finalizing a Cursor that has not been deactivated or closed. database......
Application did not close the cursor or database object that was opened here

it's a very long error .

someone told me to use c.close(); above return c, something like this:

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;
    }  

I tried it but now I get this error :

Access closed cursor

What should I do ? How should I close the cursor in this class?

Upvotes: 0

Views: 325

Answers (3)

Giru Bhai
Giru Bhai

Reputation: 14398

Close the cursor after using getRow function,i.e.

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;
    } 

and use it like this

Cursor c1 = getRow(rowId);
//use cursor 
//....
if(c1 != null)c1.close();

Note : rowId is the value of row which you want from database in long format.

Edit

Create Cursor c1; in your activity class.
check first if cursor is not null,then close it.

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

then get cursor as

 c1 = getRow(rowId);

And onStop/onDestroy close cursor as

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

Upvotes: 1

MilapTank
MilapTank

Reputation: 10076

hello i have face this same problem i have solve this problem like

when i am query and returning ArrayList or JSON object not cursor

may you need to change your query

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;
}  

this is for only returning one record for retuning set of record i am use this lib

https://github.com/commonsguy/cwac-loaderex

it will manage cursor operation itself and return us sqliteCursorLoader

Upvotes: 0

M D
M D

Reputation: 47817

You should close the Cursor in your Activity. like

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

After getting used of your DB you must close this Cursor.

Upvotes: 2

Related Questions