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?

4

1 Answer 1

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.