Android SQLite\u id列问题
我是安卓系统的新手,现在正在开发列表适配器。我知道某些类型的列表适配器需要_id列来处理数据。我提供了一个_id列,但无法使其工作。我以记事本为例,下面是我的代码: 我通过以下语句创建数据库:Android SQLite\u id列问题,android,listadapter,Android,Listadapter,我是安卓系统的新手,现在正在开发列表适配器。我知道某些类型的列表适配器需要_id列来处理数据。我提供了一个_id列,但无法使其工作。我以记事本为例,下面是我的代码: 我通过以下语句创建数据库: private static final String DB_TABLE = "radios"; public static final String KEY_NAME = "name"; public static final String KEY_URL = "url"; public static
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\U table+”(“+KEY\U 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();
}
我找不到任何问题,但仍然没有“\u id”错误之类的列。有什么想法吗?嗯,您创建表的SQL代码在我看来很好(即使您可以将列名括在引号中以示确认)。但可能您已经有一个未升级的旧表了?只是为了确保:增加DB_版本并再次执行。嗯,创建表的SQL代码在我看来很好(即使您可以将列名括在引号中以确保)。但可能您已经有一个未升级的旧表了?只是想确定一下:增加DB\u版本并再次执行。有没有办法让系统知道id列不是id而是id?我想没有办法-但是除了'\u id'列之外,您可以添加自己的'id'列。它不能是主键,但是如果你想的话,你可以使它唯一并在它上面创建一个索引。有没有一种方法可以让系统知道id列不是_id而是id?我认为没有办法,但是你可以在_id列之外添加你自己的“id”列。它不能是主键,但如果需要,可以使其唯一并在其上创建索引。