我的光标
Cursor c= db.query(DBHelper.EXAM_DATA,new String [] {DBHelper.EXAM,DBHelper.FILE}, null,null, null, null, null);
光标循环
c.moveToFirst();
do
{
name = c.getString(0);
file = c.getString(1);
Toast.makeText(this, name + " "+file ,Toast.LENGTH_LONG).show();
adapter = new SimpleCursorAdapter(this, R.layout.row_reasoning, c, new String [] {file}, new int [] {R.id.txtList});
LvReasoning.setAdapter(adapter);
}while(c.moveToNext());
Logcat显示此错误
01-20 16:33:55.154: E/AndroidRuntime(2426): FATAL EXCEPTION: main
01-20 16:33:55.154: E/AndroidRuntime(2426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.ListReasoning}: java.lang.IllegalArgumentException: column '_id' does not exist
我的表有_id列但我不知道如何解决此错误
答案 0 :(得分:1)
c.moveToFirst();
do
{
name = c.getString(0);
file = c.getString(1);
Toast.makeText(this, name + " "+file ,Toast.LENGTH_LONG).show();
adapter = new SimpleCursorAdapter(this, R.layout.row_reasoning, c, new String [] {DBHelper.FILE}, new int [] {R.id.txtList});
}while(c.moveToNext());
LvReasoning.setAdapter(adapter);
我用过这个,这就是工作。我已经通过DBHelper表字段名称和它的工作正常..但我真的不知道为什么代码不起作用...
答案 1 :(得分:0)
提供给SimpleCursorAdapter的游标必须包含名为_id的列。它存在于表中的事实是不够的,你必须选择它,所以将它包含在返回的列列表中:
Cursor c= db.query(DBHelper.EXAM_DATA,new String [] {"_id", DBHelper.EXAM, DBHelper.FILE}, null,null, null, null, null);
您还需要实例化并设置适配器一次。将这些语句移到while循环之外(如果不需要为每个游标行的内容加密,可以完全删除循环):
adapter = new SimpleCursorAdapter(this, R.layout.row_reasoning, c, new String [] {file}, new int [] {R.id.txtList});
LvReasoning.setAdapter(adapter);
对于每个游标行,适配器然后将列'file'的值映射到R.layout.row_reasoning中的R.id.txtList。