首页 > 编程语言 > 详细

java.lang.IllegalArgumentException: column '_id' does not exist

时间:2014-11-10 12:11:21      阅读:340      评论:0      收藏:0      [点我收藏+]

使用SimpleCursorAdapter显示SQLite的数据到ListView时,显示java.lang.IllegalArgumentException: column ‘_id‘ does not exist这个错误,意思是说:字段"_id"不存在。

下面我们来看看SimpleCursorAdapter的继承关系,你就会知道这是什么原因造成的:

                                                 bubuko.com,布布扣

我们可以看到SimpleCursorAdapter继承ResourseCursorAdapter,而ResourseCursorAdapter又继承CursorAdapter,那现在我们来看看CursorAdapter的描述:

Adapter that exposes data from a Cursor to a ListView widget. The Cursor must include a column named "_id" or this class will not work.

意思是说:适配器将数据从光标显示到ListView视图控件上。这个光标必须包含一个列命名为"_id"或者这个类将不会工作。

这个时候事情就很明白了,我们在查询SQLite数据库时,查询字段中没有"_id",所以光标cursor中就不存在"_id"这个字段。SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)中的from也要有一个"_id"对应的列才能成功。

所以解决办法就是:查询字段中要有“_id”,SimpleCursorAdapter的继承者的构造方法中的from也要有和查询字段相对应的“_id”,就可以用解决问题啦。

java.lang.IllegalArgumentException: column '_id' does not exist

原文:http://blog.csdn.net/u014544193/article/details/40976579

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!