Android游标与ORMLite在CursorAdapter中使用 [英] Android Cursor with ORMLite to use in CursorAdapter
问题描述
有什么办法,如何获取Cursor查询,我正在用ORMLite Dao对象处理?
ORMLite 现在支持 next()
, )的上一个()
, moveRelative(offset)
ormlite-core / com / j256 / ormlite / dao / CloseableIterator.htmlrel =noreferrer> CloseableIterator
class 。这将允许你移动底层的 Cursor
对象。
它还支持以下DAO Cursor方法:
-
dao.mapSelectStarRow(databaseResults)
返回最新行从数据库查询到select *
。这样,您可以更改光标位置(例如),然后获取当前对象。 -
dao.getSelectStarRowMapper()
提供一个映射器,您
当你建立你的使用 ORMLite 查询自己的查询,则使用 QueryBuilder
对象。 queryBuilder.prepare()
返回一个 PreparedQuery
,它由DAO中的各种方法使用。您可以调用 dao.iterator(preparedQuery)
,它将返回一个 CloseableIterator
,用于遍历结果。有一个 iterator.getRawResults()
可以访问 DatabaseResults
类。在Android下,可以将其转换为 AndroidDatabaseResults
,它具有 getCursor()
方法, code> Cursor 。
类似下面的代码:
//构建查询
QueryBuilder< Foo,String> qb = fooDao.queryBuilder();
qb.where()...;
//完成后,准备您的查询并构建一个迭代器
CloseableIterator< Foo> iterator = dao.iterator(qb.prepare());
try {
//获取在Android下可以转换的原始结果
AndroidDatabaseResults results =
(AndroidDatabaseResults)iterator.getRawResults();
Cursor cursor = results.getRawCursor();
...
} finally {
iterator.closeQuietly();
}
这有点复杂,但你是不得不在数据库后面找到被数据库抽象类隐藏的这个对象。
Is there any way, how to get Cursor for a query, which I am processing with ORMLite Dao object?
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.
这篇关于Android游标与ORMLite在CursorAdapter中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!