android*SimpleCursorAdapter *_id
16lz
2021-01-23
在使用ListViw的时候使用simpleCursorAdapter适配器显示sqlite数据,
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.persionitem, cursor, new String[{ "personid", "name", "age" }, new int[] {R.id.personid, R.id.name, R.id.age });
就出现 java.lang.IllegalArgumentException: column ‘_id’ does not exist 错误。
查询手册后,发现Cursor中未包含名为_id的列,SimpleCursorAdapter父类CursorAdapter的要求。
遇到这种情况,可以在查询语句中伪造一列名为_id的字段。
建议:sqlite的主键命名时,命名为_id,所以,当你用到sqlite的simpleCursorAdapter时,必须把数据表的主键命名为_id。
改动:1
String sql = "select personid as _id,name,age from person limit ?,?";
2 :SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.persionitem, cursor, new String[{ "_id", "name", "age" }, new int[] {R.id.personid, R.id.name, R.id.age });
更多相关文章
- android适配器的简单使用
- android 检测sqlite数据表和表中字段(列)是否存在 (转)
- 打造Android万能适配器Adapter
- android 适配器里面的按钮点击切换
- Android性能:经典ListView适配器convertView缓存及复用机制
- 一个功能强大的RecyclerViewAdapter 适配器 BaseRecyclerViewAda
- android操作sqlite3的blob字段,显示数据中的图片