1
我是Android的新手,现在在列表适配器上工作。我知道某些类型的列表适配器需要_id列来处理数据。我提供了一个_id列,但我无法使其工作。我跟着记事本例子,所以这里是我的代码:Android SQLite _id列问题
我创建这个语句DB:
private static final String DB_TABLE = "radios";
public static final String KEY_NAME = "name";
public static final String KEY_URL = "url";
public static final String KEY_ROWID = "_id";
私有静态最后弦乐DB_CREATE = “创建表” + DB_TABLE + “(” + KEY_ROWID + “整数主键自动增量”, + KEY_NAME +“text not null”,+ KEY_URL +“text not null);”;
使用dbhelper类:
private static class DBHelper extends SQLiteOpenHelper {
DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
onCreate(db);
}
}
在这里,我得到的所有记录分贝:
public Cursor getAllRadios() {
return db.query(DB_TABLE, new String[] {KEY_ROWID,KEY_NAME,KEY_URL}, null, null, null, null, null);
}
而且我的适配器:
String[] from = new String[] { DBAdapter.KEY_NAME };
int[] to = new int[] { R.id.text1 };
try{
SimpleCursorAdapter radios = new SimpleCursorAdapter(this, R.layout.list_item, cursor , from, to);
setListAdapter(radios);
}
catch(Exception e){
e.printStackTrace();
}
我无法找到任何问题,但我仍然得到没有这样的列作为“_id”错误。有任何想法吗?
有没有办法让系统知道id列不是_id而是id? – 2010-12-29 06:49:21
我认为没有办法 - 但是除了'_id'列之外,您还可以添加自己的'id'列。它不能成为主键,但是如果你愿意的话,你可以使它独一无二,并在其上创建一个索引。 – mreichelt 2010-12-29 09:06:29