1

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.

Community
  • 1
  • 1
Luis Neves
  • 1,067
  • 2
  • 10
  • 21
  • `startManagingCursor(c)` is deprecated for a reason :) – zapl Dec 07 '12 at 00:20
  • lol I know that :) but how can I solve this? Am I able to continue using my existing methods to retrieve data from sqlite with the cursorloader? if yes, how? – Luis Neves Dec 07 '12 at 00:26
  • simplest way is to use no managing cursor method. Just open and close the cursor yourself. Otherwise use a loader like the one you linked and implement the loader callbacks like in http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/ – zapl Dec 07 '12 at 00:33
  • Check this out: http://helpmeco.de/2012/3/using-an-android-cursor-loader-with-a-content-provider – Tudor Luca Dec 07 '12 at 00:44
  • @Tudor thanks for all those links, but I can google too and as I said I've read a lot about this and I don't want to use content providers (if possible) – Luis Neves Dec 07 '12 at 00:47
  • I am sorry for that. Please explain again what you really want because it appears that I didn't understand your needs properly. – Tudor Luca Dec 07 '12 at 00:50

2 Answers2

1

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.

CL.
  • 173,858
  • 17
  • 217
  • 259
Joe Malin
  • 8,621
  • 1
  • 23
  • 18
0

In my case, I was using managedQuery to return a cursor to my activity from the ContentProvider. I simply changed the call to getContentResolver().query and problem solved.

Do a Control H in Eclipse to swap out all the changes you need.

Droid Chris
  • 3,455
  • 28
  • 31