Android 光标与 ORMLite 在 CursorAdapter 中使用

发布于 2024-11-30 17:15:44 字数 52 浏览 2 评论 0原文

有什么方法可以获取我正在使用 ORMLite Dao 对象处理的查询的 Cursor 吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若水般的淡然安静女子 2024-12-07 17:15:45

如果您处于活动中并且不想乱用 QueryBuilder,请尝试以下操作,这同样有效。

Cursor cursor = getHelper().getReadableDatabase().query(tableName, projection, selection, selectionArgs, groupBy, having, sortOrder)

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.

Cursor cursor = getHelper().getReadableDatabase().query(tableName, projection, selection, selectionArgs, groupBy, having, sortOrder)
放血 2024-12-07 17:15:45

如果您的意思是使用 getHelper() 方法来访问 dao 方法 create 等,您只需继承 OrmLiteBaseActivity 即可调用它。它看起来像这样:

public class YourClass extends OrmLiteBaseActivity<YourDBHelper> {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
     ...
     getHelper().getDao().queryForAll();
     ...
  }
}

如果你的意思是用光标来处理数据库操作:我不认为你能达到它!但我不明白为什么你需要它。 ORMLite几乎具有游标的所有功能。那么你需要它做什么?

If you mean the getHelper() method to reach the dao methods create etc. you only have to inherit from the OrmLiteBaseActivity<YourDBHelper> and you can call it. It will look sth like this:

public class YourClass extends OrmLiteBaseActivity<YourDBHelper> {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
     ...
     getHelper().getDao().queryForAll();
     ...
  }
}

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?

流年里的时光 2024-12-07 17:15:44

ORMLite 现在支持 next()previous()、< code>moveRelative(offset), ... CloseableIterator。这应该允许您随意移动底层的 Cursor 对象。

它还支持以下 DAO 游标方法:


当您使用 ORMLite 构建自己的查询时,您可以使用 QueryBuilder 对象。 queryBuilder.prepare() 返回一个 PreparedQuery,它由 DAO 中的各种方法使用。您可以调用dao.iterator(preparedQuery),它将返回一个CloseableIterator,用于迭代结果。有一个 iterator.getRawResults() 可以访问 DatabaseResults 类。在 Android 下,可以将其转换为 AndroidDatabaseResults,该结果具有 getCursor() 方法以返回 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();
}

这有点复杂,但是您肯定必须在vale后面查看才能找到这个被数据库抽象类隐藏的对象。

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:


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.

十年九夏 2024-12-07 17:15:44

您是否尝试过这篇文章中格雷的一些建议?他解释了如何选择列作为另一个名称,例如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文