列“_id”不存在SimpleCursorAdapter重访
我想使用 SimpleCursorAdapter 来填充我的 ListActivity,但我遇到了经典的异常,因为我的 Cursor 中没有“_id”字段。不幸的是,这种类型的查询没有为我提供可以用作 _id 字段的字段,其形式为:
SELECT DISTINCT room from tblRates WHERE resid='ABC' AND date='2011-10-17'
由于 DISTINCT,我不能只添加 _id。那么,除了设置自定义适配器之外,我还能做什么呢?
顺便说一句,我已经看过这篇文章,所以我确实明白为什么我会收到错误。只是想知道如何使用我正在执行的查询类型获取 _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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以做这样的事情...
这将为每行添加一个值为 1 的 _id 列。另外,如果您想要该列具有不同的值而不是 1,我认为 sqlite 对于每个表都有一个隐藏的“rowid”列。
You could do something like this...
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.
除了自定义
Adapter
方法之外,您还可以使用CursorWrapper
添加您自己的_id
值。只需从1
或其他内容开始按顺序对它们进行编号,并且不要尝试将它们用作数据库中的实际键。 :-)更新
即兴...
步骤#1:创建
CursorWrapper
的子类。步骤 #2:保留包装的
Cursor
的getColumnCount()
值,此处称为N
。步骤#3:重写
getColumnCount()
以返回N+1
。步骤 #3:重写
getColumnIndex()
以返回N
作为_id
列索引。步骤#4:重写所有其他以
int columnIndex
作为参数的方法。如果索引不是N
,则将工作委托给包装的Cursor
;否则,您自己实现它(或者如果不可能或不方便并且您不需要它,则抛出RuntimeException
)。对于 getInt() 和 getLong() 实现(不确定使用哪个 CursorAdapter),返回一些可能的唯一值(例如,仅使用您的位置通过 getPosition())。步骤#5:创建子类的实例,使用
DISTINCT
子句包装Cursor
,并将其交给CursorAdapter
。Besides the custom
Adapter
approach, you could useCursorWrapper
to add your own_id
values. Just sequentially number them starting from1
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 wrappedCursor
, here referred to asN
.Step #3: Override
getColumnCount()
to returnN+1
.Step #3: Override
getColumnIndex()
to returnN
as the_id
column index.Step #4: Override all other methods that take
int columnIndex
as a parameter. If the index is notN
, delegate the work to the wrappedCursor
; otherwise, implement it yourself (or throw aRuntimeException
if it is impossible or inconvenient and you don't need it). ForgetInt()
andgetLong()
implementations (not sure whichCursorAdapter
uses), return some likely unique value (e.g., just use your position viagetPosition()
).Step #5: Create an instance of your subclass, wrapping your
Cursor
with theDISTINCT
clause, and hand that to theCursorAdapter
.在 SQLiteDatabase 中使用方法“query”可能会有所帮助。
例如,
Using method "query" in SQLiteDatabase may help.
For example,