2013-08-07 59 views
0

得到_ID到目前爲止,我得到這個如何使用SimpleCursorAdapter

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); 
    } 
} 

回答

0

只要做到這一點:

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); 
} 

}

+0

我沒有在myDBHelper – Lukap

+0

ID字段它總是聲明你的主鍵是一個好主意: _id INTEGER PRIMARY KEY AUTOINCREMENT –

+0

哪裏是你想要的'ID'場?您正在查詢「SELECT _ID,title,value」,所以_ID在您的db –

相關問題