android中SimpleCursorAdapter _id错误的问题
16lz
2021-01-23
作为一个android新手,在绑定数据的时候是这样的
ListView listview=(ListView)this.findViewById(R.id.listView1); XJDal xj=new XJDal(MainActivity.this); Cursor cur=xj.Query(); ListAdapter ad=new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, cur,new String[]{"timeflag","type"}, new int[]{android.R.id.text1,android.R.id.text2} ); listview.setAdapter(ad);
查询语句
public Cursor Query() { return db.query("XJ", new String[]{"timeflag","type"}, null, null, null, null, null); }
这个时候运行总是报错,其中有一行
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sclock/com.sclock.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
缺少了一列_id。
_id是用来干什么的?
SimpleCursorAdapter只识别_id作为主键
所以我们需要把上面查询的代码修改一下,添加一个_id的值,如果你的数据库中没有的话可以将主键 as _id。
public Cursor Query() { return db.query("XJ", new String[]{"_id","timeflag","type"}, null, null, null, null, null); }
更多相关文章
- Android 新手常见的10个误区(下)
- 强烈推荐Android新手的入门学习策略之一,Android Studio4使用配置
- Windows Phone 7 不温不火学习之《ListBox 数据与Android ListVi
- 【android工程转为lib工程后提示'R.id.xxx不能作为case语句的表