Android SimpleCursorAdapter 没有这样的列 Id
我有一个 Listview,我想用 SQLite 数据库中的信息填充它,这似乎是最实用的解决方案。 在我的调试器中,它说这是由以下原因引起的:
IllegalArgumentException 没有这样的列。 ID不存在
这是我试图用以下内容填充它的 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);
}
I have a Listview that I want to populate with information from my SQLite database with and this seemed like the most practical solution.
In my debugger it says it's caused by:
IllegalArgumentException No such column. Id does not exist
This is the java file I'm trying to populate it with:
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 file:
<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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这意味着您的数据库表中不存在列名“name”或“data”。
您可以发布表架构吗?这些列名称可用吗?
It means either column name "name" or "data" does not exist in your database table.
Can you publish the table schema? Are these column names available?
实际上,按照上面的建议声明 _id 将起作用“_id INTEGER PRIMARY KEY AUTOINCRMENT”,但是如果您不需要 _id 在表的生命周期中是唯一的,则可以省略 AUTOINCRMENT 关键字,因为“PRIMARY KEY”也会autoincrement,但在逻辑上等价于 (max(_id)+1),它保证了唯一性,这正是使用 CursorAdapter 时您想要的。
Actually declaring the _id as suggested above will work "_id INTEGER PRIMARY KEY AUTOINCREMENT", but if you don't need _id to be unique over the life of the table, you can omit the AUTOINCREMENT keyword, as the "PRIMARY KEY" will also autoincrement, but is logically equivalently to (max(_id)+1), which guarantees uniqueness which is really what you want when using the CursorAdapter.
SimpleCursorAdapter
始终需要一个 _id 字段。您的数据库包含没有 id 列或 id 具有其他列名称的表。所以卸载应用程序。并将 CREATE 语句编辑为:
SimpleCursorAdapter
always need a _id field .Your database contains table without id column or id with other column name . so uninstall app . and edit CREATE statement as :
您未在 getSpinnerData() 中执行的查询的列列表中包含
_id
。仅供参考,
您必须扩展 CursorAdapter ,它需要
_id
列。_id
仅在 CursorAdapter 中使用来确定哪一列是 id。您可以在 CursorAdapter 中覆盖此行为或将查询中的别名添加到 id。You are not including
_id
in your column list for the query you do in getSpinnerData().FYI,
You must be extending CursorAdapter which demand
_id
column._id
is used only in CursorAdapter to determine which column is id. You can override this behavior in CursorAdapter or add alias in query to id.