0

我在我的 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();
                    }
}
4

1 回答 1

0

CursorAdapter(SimpleCursorAdapter 扩展)的文档指出:

The Cursor must include a column named "_id" or this class will not work.
于 2013-03-13T17:02:25.350 回答