Android 光标与 ORMLite 在 CursorAdapter 中使用
有什么方法可以获取我正在使用 ORMLite Dao 对象处理的查询的 Cursor 吗?
Is there any way to get Cursor for a query, which I am processing with ORMLite Dao object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您处于活动中并且不想乱用 QueryBuilder,请尝试以下操作,这同样有效。
If you're in an Activity and don't want to mess around with the QueryBuilder give the following a go, which is just as effective.
如果您的意思是使用
getHelper()
方法来访问 dao 方法 create 等,您只需继承OrmLiteBaseActivity
即可调用它。它看起来像这样:如果你的意思是用光标来处理数据库操作:我不认为你能达到它!但我不明白为什么你需要它。 ORMLite几乎具有游标的所有功能。那么你需要它做什么?
If you mean the
getHelper()
method to reach the dao methods create etc. you only have to inherit from theOrmLiteBaseActivity<YourDBHelper>
and you can call it. It will look sth like this:If you mean the cursor to handle database operation: I don't think that you can reach it! But I don't understand why you should need it. ORMLite has nearly all functions of the cursor. So what do you need it for?
ORMLite 现在支持
next()
、previous()
、< code>moveRelative(offset), ...CloseableIterator
类。这应该允许您随意移动底层的 Cursor 对象。它还支持以下 DAO 游标方法:
dao.mapSelectStarRow(databaseResults)
返回来自的最新行数据库结果来自对select *
的查询。通过这个,您可以更改光标位置(例如),然后获取当前对象。dao.getSelectStarRowMapper( )
提供一个映射器,您可以使用它来映射 Dao 之外的对象。当您使用 ORMLite 构建自己的查询时,您可以使用
QueryBuilder
对象。queryBuilder.prepare()
返回一个PreparedQuery
,它由 DAO 中的各种方法使用。您可以调用dao.iterator(preparedQuery)
,它将返回一个CloseableIterator
,用于迭代结果。有一个iterator.getRawResults()
可以访问DatabaseResults
类。在 Android 下,可以将其转换为AndroidDatabaseResults
,该结果具有getCursor()
方法以返回 AndroidCursor
。类似下面的代码:
这有点复杂,但是您肯定必须在vale后面查看才能找到这个被数据库抽象类隐藏的对象。
ORMLite now supports
next()
,previous()
,moveRelative(offset)
, ... methods on theCloseableIterator
class. This should allow you to move the underlyingCursor
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 aPreparedQuery
which is used by various methods in the DAO. You can calldao.iterator(preparedQuery)
which will return aCloseableIterator
which is used to iterate through the results. There is aiterator.getRawResults()
to get access to theDatabaseResults
class. Under Android, this can be cast to anAndroidDatabaseResults
which has agetCursor()
method on it to return the AndroidCursor
.Something like the following code:
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.
您是否尝试过这篇文章中格雷的一些建议?他解释了如何选择列作为另一个名称,例如 select id as _id。
Did you try some of Gray's advice from this post? He explains how you can select a column as another name, such as, select id as _id.