【问题标题】:Help with SimpleCursorAdapter and ListviewSimpleCursorAdapter 和 Listview 的帮助
【发布时间】:2011-05-28 20:04:14
【问题描述】:

我被 SimpleCursorAdapter 卡住了,我正在调用我的本地 SQLite DB 并将其放入一个游标中,然后将其传递给 SimpleCursorAdapter。

出于同样的原因,Log Cat 一直在下面显示此错误。我不知道发生了什么,我已经为此工作了 6 个小时,我没想到 SimpleCursorAdapter 会这么难理解。

05-28 19:47:27.524: ERROR/AndroidRuntime(9353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyneaxis.android.mpg/com.nyneaxis.android.mpg.userInfo}: java.lang.IllegalArgumentException: column '_id' does not exist


        setArray();
    rec.open();
        Cursor c = rec.getAllVeh();
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.userinfo, c, new String[]{c.getString(1)}, new int[]{R.id.nameTxtL});
        this.setListAdapter(adapter);
    rec.close();

//数据适配器

public Cursor getAllVeh() {
    try{
    return db.query(database_table, new String[] { key_rowid, vehicle_name,
            year, make, model, style, vin, plate, notes }, null, null,
            null, null, null);
    }finally{

    }
}

好的,我已将代码修改为 rawQuery,但我再次收到此错误:

05-28 22:41:48.876: 错误/AndroidRuntime(1359): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.nyneaxis.android.mpg/com.nyneaxis.android.mpg.userInfo}: android.database.CursorIndexOutOfBoundsException: 请求索引-1,大小为0

private static final String db_sel = "SELECT id as _id, vehicle_name FROM vehicle";

public Cursor getAllVeh() {
        try{
            return db.rawQuery(db_sel, null);
        /*return db.query(database_table, new String[] { key_rowid, vehicle_name,
                year, make, model, style, vin, plate, notes }, null, null,
                null, null, null);*/
        }finally{

        }
    }

【问题讨论】:

  • 好的,'key_rowid' 是什么意思?
  • 查看我的答案的编辑,解释使用 rawQuery 为 _id 列设置别名。
  • 你在哪里提到表的名称?

标签: android


【解决方案1】:

查看我对这个问题的回答Android: column '_id' does not exist problem。它解释了对 _id 列的需求以及如果您的数据库表没有具有该名称的列时如何为其命名。

****EDIT:**** 要为数据库中包含“唯一标识符”的列设置别名,您需要使用db.rawQuery(...) 而不是db.query(...)db.rawQuery(...) 方法采用 SQL 字符串,它允许您将列名别名为适配器所需的“_id”。示例...

Cursor c = db.rawQuery("SELECT <my_unique_column_name> as _id, vehicle_name, ... FROM vehicles");

在上面,将&lt;my_unique_column_name&gt; 替换为车辆表中包含唯一标识符的列的实际名称。此外,对您请求数据的任何其他列使用实际的列名。

【讨论】:

  • 现在我得到这个错误:05-28 20:17:09.634: ERROR/AndroidRuntime(10305): Caused by: android.database.sqlite.SQLiteException: no such column: _id: , while compile : SELECT _id, vehicle_name, year, make, model, style, vin, plate, notes FROM vehicle
  • @Brandon:使用别名,即“SELECT id as _id,...”等
  • 如果我不理解这一点,我很抱歉。有没有办法提供一个例子?我真的不明白。
  • 我使用了别名,但仍然收到错误消息。这是我检索数据的代码。以上
  • @Brandon:也许是我不理解。您能否确认该表是否有一个名为“_id”的列还是名为“id”的列(您对 Femi 的回答的评论让我认为它有一个名为“id”的列。您能否编辑您的原始问题以显示代码你的光标查询?
【解决方案2】:

好吧,正如错误所说,您的数据库表定义缺少 SimpleCursorAdapter 期望用作 ID 列的默认 _id 列。您的数据库表定义为什么?将您正在使用的CREATE TABLE 语句添加到您的问题中。

SimpleCursorAdapter 依赖于 _id 列的存在:如果您没有它,那么您将收到该错误。

【讨论】:

  • 你知道我拥有它只是 id,我认为这并不重要。为什么会有所不同?
  • 我不使用 CREATE TABLE 我从文件中复制了一个数据库,其中包含 _id 列,但出现以下错误。
  • 啊,显示rec.getAllVeh();的代码:应该很容易修复。
【解决方案3】:

感谢大家的帮助。我设法以不同的方式做到这一点,它似乎就像一种魅力。如果有人有任何问题或建议,请告诉我。

Cursor c = rec.getAllVeh();

while (c.moveToNext()) {
String vehName = c.getString(1);
vehInfo.add(vehName);
}

//put information into the addapter for listview
adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_single_choice, vehInfo);

//applies adapter to listview
list.setAdapter(adapter);

【讨论】:

  • 嗨,你的方法对我来说看起来很不错,但你能告诉我什么是 vehInfo,是 StringBuilder、StringBuffer 还是向量或一些列表?
  • 你如何将这些字符串设置为 xml 中定义的特定 id?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多