Android Cursor with ORMLite to use in CursorAdapter

折月煮酒 提交于 2019-12-28 01:43:46

问题


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


回答1:


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 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.

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.




回答2:


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.




回答3:


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)



回答4:


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?



来源:https://stackoverflow.com/questions/7159816/android-cursor-with-ormlite-to-use-in-cursoradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!