带有 ORMLite 的 Android Cursor 在 CursorAdapter 中使用 [英] Android Cursor with ORMLite to use in CursorAdapter
问题描述
有什么方法可以获取我正在使用 ORMLite Dao 对象处理的查询的 Cursor?
Is there any way, how to get Cursor for a query, which I am processing with ORMLite Dao object?
推荐答案
ORMLite 现在支持 next()
, previous()
, moveRelative(offset)
, ... CloseableIterator
类.这应该允许您随意移动底层 Cursor
对象.
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.
它还支持以下 DAO Cursor 方法:
It also supports the following DAO Cursor methods:
dao.mapSelectStarRow(databaseResults)
将查询的数据库结果中的最新行返回到select *
.有了这个,您可以更改光标位置(例如),然后获取当前对象.dao.getSelectStarRowMapper()
提供了一个映射器,可以用来映射Dao之外的对象.
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.
当您使用 ORMLite 构建自己的查询时,您可以使用 QueryBuilder
对象.queryBuilder.prepare()
返回一个 PreparedQuery
,它被 DAO 中的各种方法使用.您可以调用 dao.iterator(preparedQuery)
它将返回一个 CloseableIterator
用于迭代结果.有一个 iterator.getRawResults()
可以访问 DatabaseResults
类.在 Android 下,这可以转换为 AndroidDatabaseResults
,它有一个 getCursor()
方法来返回 Android Cursor
.
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
.
类似于以下代码:
// 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.
这篇关于带有 ORMLite 的 Android Cursor 在 CursorAdapter 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!