How to use Ormlite with LoaderManager

android, ormlite

Solution

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

Problem

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?

Original source

Related problems