3

我有一个 Listview,我想用我的 SQLite 数据库中的信息填充它,这似乎是最实用的解决方案。在我的调试器中,它说它是由以下原因引起的:

IllegalArgumentException 没有这样的列。身份证不存在

这是我试图填充它的java文件:

    data        = new MyData(this);
    ListView lv = (ListView) findViewById(R.id.list);

    ListAdapter adapter = new SimpleCursorAdapter(
                                this,
                                R.layout.list, 
                                data.selectData(), 
                                new String[] {
                                    "name",
                                    "title"
                                },
                                new int[] {
                                    R.id.name,
                                    R.id.title
                                }
    );
    lv.setAdapter(adapter);

R.layout.list xml 文件:

<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
    <TextView android:id="@+id/name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="8dip"/>
    <TextView android:id="@+id/title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

</LinearLayout>

public Cursor selectData() {

    return db.query("tbl_mydata", new String[] {"name", "abb" }, null, null, null, null, null);
}
4

4 回答 4

5

您没有将在getSpinnerData()_id中执行的查询包含在列列表中。

供参考,

您必须扩展CursorAdapter哪个需求_id列。

_id仅在 CursorAdapter 中用于确定哪一列是 id。您可以在 CursorAdapter 中覆盖此行为或将查询中的别名添加到 id。

于 2011-11-11T06:36:55.180 回答
1

SimpleCursorAdapter总是需要一个 _id 字段。

您的数据库包含没有 id 列或 id 具有其他列名称的表。所以卸载应用程序。并将 CREATE 语句编辑为:

  "CREATE TABLE IF NOT EXISTS contact_data( _id INTEGER PRIMARY KEY AUTOINCREMENT,  something ,............ )
于 2011-11-11T06:43:37.173 回答
0

这意味着您的数据库表中不存在列名“名称”或“数据”。您可以发布表架构吗?这些列名可用吗?

于 2011-11-11T06:37:22.413 回答
0

实际上,按照上面的建议声明 _id 将起作用“_id INTEGER PRIMARY KEY AUTOINCREMENT”,但如果您不需要 _id 在表的整个生命周期中是唯一的,则可以省略 AUTOINCREMENT 关键字,因为“PRIMARY KEY”也会autoincrement,但在逻辑上等价于 (max(_id)+1),它保证了唯一性,这正是您在使用 CursorAdapter 时想要的。

于 2012-04-24T14:08:20.167 回答