Android Cursor with ORMLite to use in CursorAdapter
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:
-
dao.mapSelectStarRow(databaseResults)
Return the latest row from the database results from a query toselect *
. With this you can change the cursor location (for example) and then get the current object. -
dao.getSelectStarRowMapper()
Provides a mapper that you can use to map the object outside of the Dao.
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.
sealskej
Updated on June 03, 2022Comments
-
sealskej about 2 years
Is there any way to get Cursor for a query, which I am processing with ORMLite Dao object?
-
sealskej almost 13 yearsI know about this, but I need to get Cursor to use it in CursorAdapter.
-
max4ever over 12 yearshow can i obtain the databaseConnection?
-
Gray almost 12 yearsI've changed this answer to show how it can be done without needed the database connection.
-
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 withOrmLiteBaseListActivity
? -
Gray over 10 yearsSorry @mattblang. I'm not an Android developer. I'd ask that question on the mailing list: groups.google.com/forum/#!forum/ormlite-android
-
Dakait over 10 years@Gray your contribution on SO specifically regarding ORMLite is much appreciated, tnx
-
Mark over 10 years@Gray when using this strategy in and handing the cursor off to a
SimpleCursorAdapter
orCursorAdapter
subclass of my own used with an Android Grid/ListView, I get aStaleDataException
-- I noticed commenting out theiterator.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 about 10 years@Mark Yes, same problem here. What did you end up doing?
-
AgentKnopf almost 10 yearsSince you can name your columns in ormlite I guess why not just name them _id, rather than doing the select as :) ?
-
Simon.Ponder almost 10 yearsSure, you can, but in an instance where renaming them is not an option, you simply use the sql syntax of "as";)