Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ormlite with LoaderManager

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?

like image 505
Giuseppe Avatar asked Mar 09 '13 07:03

Giuseppe


1 Answers

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

like image 162
Grimmy Avatar answered Nov 15 '22 22:11

Grimmy