9

I am using successfully OrmLite in my Android apps. I am moving my apps to the new CursorLoader logic and I would like to know how to use Ormlite with a CursorLoader without to have a ContentProvider.

Is't possible?

Giuseppe
  • 1,079
  • 13
  • 31
  • lookin in to this as well found these threads let me know if u find anything else usefull http://stackoverflow.com/questions/7159816/android-cursor-with-ormlite-to-use-in-cursoradapter http://stackoverflow.com/questions/12416964/ormlite-with-cursoradapter-in-android https://groups.google.com/forum/#!topic/ormlite-android/6am7a3l3IeY https://groups.google.com/forum/?fromgroups=#!topic/ormlite-user/SbRoHIov5pI – baboo Mar 09 '13 at 09:55
  • +baboo thanks for your research, but before to post here I have already tried to find an answer.... – Giuseppe Mar 09 '13 at 09:58
  • Hi! I'm having your same troubles.. How have you solved.. if you have solved? – gipinani Dec 05 '13 at 07:42
  • 1
    https://github.com/amityadav1984/ORMLite-Tutorial – Amit Yadav Oct 05 '14 at 13:20

1 Answers1

4

I think the best solution will be implement subclass for CursorLoader, and in loadInBackground() get and return cursor from ORM. In my case it was something like this

@Override
public Cursor loadInBackground() {
    ...
    Dao<Account, String> dao = mHelper.getDao();
    QueryBuilder<Account, String> qb = dao.queryBuilder();                    
    CloseableIterator<Account> iterator = dao.iterator(qb.prepare());
    try {
        AndroidDatabaseResults results =
        (AndroidDatabaseResults)iterator.getRawResults();
        cursor = results.getRawCursor();               
    }
    catch(Exception ex){                    
        ex.printStackTrace();
    }
    ...
    return cursor;
}

Than you can use this Loader just as other loaders. You can look at full example with custom AsyncTaskLoader here: Example

Grimmy
  • 2,041
  • 1
  • 17
  • 26