我在我的SQLlite数据库中输入了一些值,然后在gridview中检索它们。我收到错误“column'_id'”不存在。但我没有在数据库中使用任何_id命名列。我怎样才能让它发挥作用?我认为SimpleCursor Adapter存在一些问题。
GridView gv = (GridView)findViewById(R.id.gridView1);
try
{
db1 = openOrCreateDatabase("record.db", Context.MODE_PRIVATE,null);
db1.execSQL("CREATE TABLE IF NOT EXISTS stu(ID INTEGER primary Key,NAME VARCHAR foriegn key,age VARCHAR);");
db1.execSQL("INSERT INTO stu VALUES (1,'mona','123');");
Cursor c = db1.rawQuery("SELECT * FROM stu", null);
if(c!= null){
if (c.moveToFirst()) {
do {
String[] cols = new String[]{c.getString(c.getColumnIndex("ID")),c.getString(c.getColumnIndex("NAME"))};
int[] views = new int[] {R.id.textView1,R.id.textView2};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c, cols, views);
gv.setAdapter(adapter);
}while(c.moveToNext());}
}} catch(Exception e){
Toast.makeText(getBaseContext(),"errord"+ e.getMessage(), Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
CursorAdapter(SimpleCursorAdapter扩展)的文档声明:
The Cursor must include a column named "_id" or this class will not work.