【问题标题】:How to get _ID with SimpleCursorAdapter如何使用 SimpleCursorAdapter 获取 _ID
【发布时间】:2013-08-07 12:09:27
【问题描述】:

到目前为止,我得到了这个

  db = new MYDBHelper(this);
    constantsCursor = db.getReadableDatabase().rawQuery(
            "SELECT _ID, title, value " + "FROM constants ORDER BY title", null);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, constantsCursor, new String[] { DBHelperMen2.TITLE, DBHelperMen2.VALUE }, new int[] {R.id.item_name, R.id.item_points });
listata = (ListView) findViewById(R.id.listView1);
listata.setAdapter(adapter);

但我还有一个名为 item_number 的字段。我这个字段我要写_ID号

我该怎么做?

new SimpleCursorAdapter(this, R.layout.item, constantsCursor, new String[] { **???** ,MYDBHelper.TITLE, MYDBHelper.VALUE }, new int[] {**R.id.item_number** , R.id.item_name, R.id.item_points });

这里是 MYDBHelper

public class MYDBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "db";
    static final String TITLE = "title";
    static final String VALUE = "value";

    public MYDBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, value TEXT);");
    }



        @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS constants");
        onCreate(db);
    }
}

【问题讨论】:

    标签: android adapter simplecursoradapter android-adapter


    【解决方案1】:

    只需这样做:

    ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, constantsCursor, new String[] { DBHelperMen2.ID, DBHelperMen2.TITLE, DBHelperMen2.VALUE }, new int[] {R.id.item_number, R.id.item_name, R.id.item_points });
    

    在 DBHelperMen2.ID 中,ID 是您的数据库中列的名称

    这段代码应该可以工作:

    public class MYDBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "db";
    
    static final String TABLE_NAME = "constants";
    static final String ID = "_id";
    
    static final String TITLE = "title";
    static final String VALUE = "value";
    
    public MYDBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
    
        db.execSQL("CREATE TABLE "+ TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TITLE + " TEXT, " + VALUE + " TEXT);");
    }
    
    
    
        @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    

    }

    【讨论】:

    • myDBHelper 中没有 ID 字段
    • 将主键声明为:_id INTEGER PRIMARY KEY AUTOINCREMENT
    • 你想要的ID字段在哪里?您正在查询“SELECT _ID, title, value”所以 _ID 在您的数据库中
    • 我发布了我的 MYDBHelper 课程
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多