Search code examples
androidsqlitesqliteopenhelperandroid-cursorandroid-cursorloader

Android HoneyComb - Requering already closed cursor


I read lots of questions about this problem but I can't find the right direction to my problem, maybe because I'm noob and feeling desperated to find the right way...

As far as I read, the "requering already closed cursor" problem happens since HoneyComb because startManagingCursor is deprecated. And since HoneyComb we should use the CursorLoader that works with ContentProvider (which I don't use and I don't want to), and found that someone wrote a class to use CursorLoader without ContentProvider (here).

On my application, I've a mySQLiteHelper class which I've lots of methods to retrieve data from SQLite like this one:

    public Cursor listaBeneficios(String id) {
    String where = "proposta=?";
    String[] whereArgs = {id};
    String[] campos = {"pessoa"};
    Cursor c = db.query(TABLE_BENEFICIOS, campos, where, whereArgs, null, null, null, null);
    if (c.moveToFirst()) {
        return c;
    } else {
        return null;
    }
}

And on my Activity, I could read the data like this:

    db = new repositorio(MainActivity.this);
    Cursor c = db.listaBeneficios(PROPOSTA);
    startManagingCursor(c);
    address.setText(c.getString(c.getColumnIndex("address")));
    db.close();

But I noticed that it gives the infamous "trying to requery a allready closed cursor" everytime that I hit the back button and return to Activity A. I've allready tried to stopManagingCursor and close() and not use startManagingCursor. This is driving me nuts.

Is there any available simple examples of using CursorLoaders ? Am I able to continue using the methods (ie, the listaBeneficios() above) above if I implement CursorLoaders? Is this class helpful for me? how can I implement it?

Any kind help is appreciated.


Solution

  • A CursorLoader won't work because it requires a content URI. However, you can always use a Loader or AsyncTaskLoader. The documentation for these classes contains examples for how to use them.