Android Cursor with ORMLite to use in CursorAdapter

17,199

ORMLite now supports next(), previous(), moveRelative(offset), ... methods on the CloseableIterator class. This should allow you to move the underlying Cursor object around at will.

It also supports the following DAO Cursor methods:


When you are building your own query with ORMLite, you use the QueryBuilder object. queryBuilder.prepare() returns a PreparedQuery which is used by various methods in the DAO. You can call dao.iterator(preparedQuery) which will return a CloseableIterator which is used to iterate through the results. There is a iterator.getRawResults() to get access to the DatabaseResults class. Under Android, this can be cast to an AndroidDatabaseResults which has a getCursor() method on it to return the Android Cursor.

Something like the following code:

// build your query
QueryBuilder<Foo, String> qb = fooDao.queryBuilder();
qb.where()...;
// when you are done, prepare your query and build an iterator
CloseableIterator<Foo> iterator = dao.iterator(qb.prepare());
try {
   // get the raw results which can be cast under Android
   AndroidDatabaseResults results =
       (AndroidDatabaseResults)iterator.getRawResults();
   Cursor cursor = results.getRawCursor();
   ...
} finally {
   iterator.closeQuietly();
}

This is a bit complicated but you are definitely having to peer behind the vale to get to this object which is hidden by the database abstraction classes.

Share:
17,199
sealskej
Author by

sealskej

Updated on June 03, 2022

Comments

  • sealskej
    sealskej about 2 years

    Is there any way to get Cursor for a query, which I am processing with ORMLite Dao object?

  • sealskej
    sealskej almost 13 years
    I know about this, but I need to get Cursor to use it in CursorAdapter.
  • max4ever
    max4ever over 12 years
    how can i obtain the databaseConnection?
  • Gray
    Gray almost 12 years
    I've changed this answer to show how it can be done without needed the database connection.
  • theblang
    theblang over 10 years
    @Gray What is the best way to inflate the list_item views using the cursor? Also, is a CursorAdapter the best thing to use with OrmLiteBaseListActivity?
  • Gray
    Gray over 10 years
    Sorry @mattblang. I'm not an Android developer. I'd ask that question on the mailing list: groups.google.com/forum/#!forum/ormlite-android
  • Dakait
    Dakait over 10 years
    @Gray your contribution on SO specifically regarding ORMLite is much appreciated, tnx
  • Mark
    Mark over 10 years
    @Gray when using this strategy in and handing the cursor off to a SimpleCursorAdapter or CursorAdapter subclass of my own used with an Android Grid/ListView, I get a StaleDataException -- I noticed commenting out the iterator.closeQuietly() prevents this from happening. What would you suggest to work-around this -- apart from keeping the iterator around until I'm done with the cursor?
  • theblang
    theblang about 10 years
    @Mark Yes, same problem here. What did you end up doing?
  • AgentKnopf
    AgentKnopf almost 10 years
    Since you can name your columns in ormlite I guess why not just name them _id, rather than doing the select as :) ?
  • Simon.Ponder
    Simon.Ponder almost 10 years
    Sure, you can, but in an instance where renaming them is not an option, you simply use the sql syntax of "as";)