列“_id”不存在SimpleCursorAdapter重访

发布于 2024-12-10 06:04:33 字数 466 浏览 4 评论 0原文

我想使用 SimpleCursorAdapter 来填充我的 ListActivity,但我遇到了经典的异常,因为我的 Cursor 中没有“_id”字段。不幸的是,这种类型的查询没有为我提供可以用作 _id 字段的字段,其形式为:

SELECT DISTINCT room from tblRates WHERE resid='ABC' AND date='2011-10-17'

由于 DISTINCT,我不能只添加 _id。那么,除了设置自定义适配器之外,我还能做什么呢?

顺便说一句,我已经看过这篇文章,所以我确实明白为什么我会收到错误。只是想知道如何使用我正在执行的查询类型获取 _id:

Android列“_id”不存在?

I want to use the SimpleCursorAdapter to fill in my ListActivity, but I'm getting the classic exception because there is no '_id' field in my Cursor. Unfortunately, this type of query does not provide me with a field that I can use as my _id field being of the form:

SELECT DISTINCT room from tblRates WHERE resid='ABC' AND date='2011-10-17'

Because of the DISTINCT I can't just add in the _id. So what do I do short of setting up a custom Adapter?

Btw, I have seen this post already so I do understand why I'm getting the error. Just wondering how to get an _id in there with the type of query I'm doing:

Android column '_id' does not exist?

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

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

发布评论

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

评论(3

最美的太阳 2024-12-17 06:04:33

你可以做这样的事情...

SELECT DISTINCT room, 1 _id from tblRates WHERE resid='ABC' AND date='2011-10-17'

这将为每行添加一个值为 1 的 _id 列。另外,如果您想要该列具有不同的值而不是 1,我认为 sqlite 对于每个表都有一个隐藏的“rowid”列。

You could do something like this...

SELECT DISTINCT room, 1 _id from tblRates WHERE resid='ABC' AND date='2011-10-17'

This will add an _id column with a value of 1 for each row. Also I think sqlite has something like a hidden "rowid" column for each table if you want distinct values for the column instead of a 1.

在你怀里撒娇 2024-12-17 06:04:33

那么除了设置自定义适配器之外我还能做什么呢?

除了自定义 Adapter 方法之外,您还可以使用 CursorWrapper 添加您自己的 _id 值。只需从 1 或其他内容开始按顺序对它们进行编号,并且不要尝试将它们用作数据库中的实际键。 :-)


更新

即兴...

步骤#1:创建 CursorWrapper 的子类。

步骤 #2:保留包装的 CursorgetColumnCount() 值,此处称为 N

步骤#3:重写getColumnCount()以返回N+1

步骤 #3:重写 getColumnIndex() 以返回 N 作为 _id 列索引。

步骤#4:重写所有其他以 int columnIndex 作为参数的方法。如果索引不是N,则将工作委托给包装的Cursor;否则,您自己实现它(或者如果不可能或不方便并且您不需要它,则抛出 RuntimeException)。对于 getInt() 和 getLong() 实现(不确定使用哪个 CursorAdapter),返回一些可能的唯一值(例如,仅使用您的位置通过 getPosition())。

步骤#5:创建子类的实例,使用 DISTINCT 子句包装 Cursor,并将其交给 CursorAdapter

So what do I do short of setting up a custom Adapter?

Besides the custom Adapter approach, you could use CursorWrapper to add your own _id values. Just sequentially number them starting from 1 or something, and don't attempt to use them as actual keys in your database. :-)


UPDATE

Off the cuff...

Step #1: Create a subclass of CursorWrapper.

Step #2: Hang onto the getColumnCount() value of the wrapped Cursor, here referred to as N.

Step #3: Override getColumnCount() to return N+1.

Step #3: Override getColumnIndex() to return N as the _id column index.

Step #4: Override all other methods that take int columnIndex as a parameter. If the index is not N, delegate the work to the wrapped Cursor; otherwise, implement it yourself (or throw a RuntimeException if it is impossible or inconvenient and you don't need it). For getInt() and getLong() implementations (not sure which CursorAdapter uses), return some likely unique value (e.g., just use your position via getPosition()).

Step #5: Create an instance of your subclass, wrapping your Cursor with the DISTINCT clause, and hand that to the CursorAdapter.

青春有你 2024-12-17 06:04:33

在 SQLiteDatabase 中使用方法“query”可能会有所帮助。

例如,

SQLiteDatabase db = openHelper.getReadableDatabase();
Cursor c = db.query(
             "tblRates", 
             new String[] { "_id", "room" }, 
             "resid=? AND date=?", 
             new String[] { "ABC", "2010-10-17" }, 
             "room", 
             null, 
             "room");
SimpleCursorAdapter a = new SimpleCursorAdapter(
                           getActivity(), 
                           android.R.layout.simple_dropdown_item_1line, 
                           c, 
                           new String[] { "room" }, 
                           new int[] { android.R.id.text1 }, 
                           0);

Using method "query" in SQLiteDatabase may help.

For example,

SQLiteDatabase db = openHelper.getReadableDatabase();
Cursor c = db.query(
             "tblRates", 
             new String[] { "_id", "room" }, 
             "resid=? AND date=?", 
             new String[] { "ABC", "2010-10-17" }, 
             "room", 
             null, 
             "room");
SimpleCursorAdapter a = new SimpleCursorAdapter(
                           getActivity(), 
                           android.R.layout.simple_dropdown_item_1line, 
                           c, 
                           new String[] { "room" }, 
                           new int[] { android.R.id.text1 }, 
                           0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文