我在Android的新手,在列表中的适配器的工作现在。我知道有些类型列表适配器需要_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 final String KEY_ROWID = "_id";

私有静态最后字符串DB_CREATE =         “创建表” + DB_TABLE + “( ”+ KEY_ROWID +“ 整数主键自动增量,”         + KEY_NAME + “文本不为空, ”+ KEY_URL +“ 文本不为空);”;

使用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” 错误。任何想法?

有帮助吗?

解决方案

嗯,用于创建表的SQL代码看起来好像没什么问题(即使你可以用引号括起来的列名只是要确定)。但是,也许你已经有未升级的现有旧表?只是可以肯定:增量DB_VERSION并再次执行

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top