ORMLite与CursorAdapter的Andr​​oid中 [英] ORMLite with CursorAdapter in Android

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

问题描述

我修改我的Andr​​oid应用程序与ORMLite工作,而且目前使用的一些CursorAdapters的,我很希望保留,企图以最小化的编码。

I am modifying my Android app to work with ORMLite, and it currently uses a number of CursorAdapters, which I quite want to keep in an attempt to minimise coding.

我不是100%肯定,但牛逼看来,当ORMLite会在数据库ID字段,它总是使用 ID ,而CursorAdapter的需要 _id

I'm not 100% sure but t appears that when ORMLite creates an id field in the db, it always uses id, whereas CursorAdapter needs _id.

有可能得到这一轮使用类似下面的查询:

It is possible to get round this using a query like the following:

select id as _id ......

Dao.queryRaw()方法返回一个列表,而不是一个游标,所以我做的方式是打开另一个SQLiteOpenHelper数据库连接,并使用 rawQuery()

but the Dao.queryRaw() method returns a list, not a Cursor, so the way I have done it is to open another SQLiteOpenHelper database connection and use rawQuery().

这个工作,但有没有做它在所有的任何更好的办法?这似乎矫枉过正有两个单独的数据库连接,或许以后储存起来麻烦。

This works, but are there any better ways of doing it at all? It seems overkill to have two separate database connections, and perhaps storing up trouble later.

推荐答案

您的评论表明您已经回答了你的问题。你当然可以创建一个名为使用_id列 ORMLite

Your comments indicate that you've already answered you problem. You can certainly create a column named "_id" using ORMLite:

@DatabaseField(generatedId = true)
private int _id;

@DatabaseField(generatedId = true, columnName = "_id")
private int id;

如果您正在使用光标取值,那么你可能想看看在末页() MOVEABSOLUTE(...)的 DatabaseResults 的方法。另外,在<一href="http://ormlite.com/javadoc/ormlite-android/com/j256/ormlite/android/AndroidDatabaseResults.html"相对=nofollow> AndroidDatabaseResults (可以转换为)也有一个 getRawCursor()方法,它返回底层光标对象,并有额外的 getCount将()为getPosition()的方法。

If you are working with Cursors then you may want to take a look at the last() and moveAbsolute(...) methods on the DatabaseResults class. Also, the AndroidDatabaseResults (which you can cast to) also has a getRawCursor() method which returns the underlying Cursor object and has additional getCount() and getPosition() methods.

下面是关于ORMLite一些更多的信息和光标 S:

Here are some more information about ORMLite and Cursors:

Android的光标与ORMLite在CursorAdapter的

您可以访问光标使用类似以下内容:

You can get access to the Cursor using something like the following:

// 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();
}

这篇关于ORMLite与CursorAdapter的Andr​​oid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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