Android的游标ORMLite在CursorAdapter的使​​用 [英] Android Cursor with ORMLite to use in CursorAdapter

查看:597
本文介绍了Android的游标ORMLite在CursorAdapter的使​​用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法,如何让光标进行查询,而我与ORMLite DAO对象处理?

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

推荐答案

ORMLite 现在支持下一个() previous() MOVERELATIVE(补偿) ...方法上<一href="http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/dao/CloseableIterator.html"><$c$c>CloseableIterator类。这应该可以让您将底层光标对象周围的意愿。

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游标的方法:

It also supports the following DAO Cursor methods:

  • <一个href="http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/dao/Dao.html#mapSelectStarRow%28com.j256.ormlite.support.DatabaseResults%29"><$c$c>dao.mapSelectStarRow(databaseResults)返回数据库中结果的最新行从查询到选择* 。有了这个,你可以改变光标位置(例如),然后获取当前对象。
  • <一个href="http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/dao/Dao.html#getSelectStarRowMapper%28%29"><$c$c>dao.getSelectStarRowMapper()提供您可以使用该对象映射道以外的映射。
  • dao.mapSelectStarRow(databaseResults) Return the latest row from the database results from a query to select *. 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 光标

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.

像下面这样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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆