如何从表中获取_id字段并将其放入ListView
我在这里有点困惑。
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.bugs_list_item, itemCursor,
new String[] {db.KEY_ROWID, db.BUGS_DATE, db.BUGS_DESCRIPTION},
new int[] {R.id.bug_id, R.id.bug_date, R.id.bug_description});
这基本上向我显示了一个带有日期和描述但没有 id 的 ListView(我只是用空格代替 id)。 _id 字段是主键,它是一个整数。
查看它在 img 上的外观
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/bug_id"
android:layout_width="5dip"
android:layout_height="wrap_content"
android:padding="3dip"
>
</TextView>
<TextView
android:id="@+id/bug_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="right"
>
</TextView>
<TextView
android:id="@+id/bug_description"
android:layout_width="180dip"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="right"
>
</TextView>
</TableRow>
</TableLayout>
创建表查询:
BUGS_CREATE = "CREATE TABLE bugs (_id INTEGER NOT NULL PRIMARY KEY, date DATE DEFAULT (DATETIME('NOW')) NOT NULL, description TEXT) ";
I'm beeing bit confused here.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.bugs_list_item, itemCursor,
new String[] {db.KEY_ROWID, db.BUGS_DATE, db.BUGS_DESCRIPTION},
new int[] {R.id.bug_id, R.id.bug_date, R.id.bug_description});
This basically shows me a ListView with date and description but no id (I just get blank space in place of id). _id field is primary key, it's an integer.
See how it looks on the img
XML file:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/bug_id"
android:layout_width="5dip"
android:layout_height="wrap_content"
android:padding="3dip"
>
</TextView>
<TextView
android:id="@+id/bug_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="right"
>
</TextView>
<TextView
android:id="@+id/bug_description"
android:layout_width="180dip"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="right"
>
</TextView>
</TableRow>
</TableLayout>
create table query:
BUGS_CREATE = "CREATE TABLE bugs (_id INTEGER NOT NULL PRIMARY KEY, date DATE DEFAULT (DATETIME('NOW')) NOT NULL, description TEXT) ";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的“id-field”的名称必须是“_id”,这一点非常重要,否则它可能无法工作。
It's very important that your "id-field" does have the name "_id", else it might not work.
列表视图行的 XML 是什么样的?你设置的ID正确吗?
编辑:
由于您的 XML 文件似乎设置正确,我现在相信您的问题是其他问题。可能是 bug_id 字段的数据未从数据库中正确存储或检索。
这是来自 android 文档:
'请确保包含一个名为“_id”的整数列(带有常量 _ID)作为记录的 ID。无论您是否有其他在所有记录中也是唯一的字段(例如 URL),您都应该拥有此字段。如果您使用的是 SQLite 数据库,则 _ID 字段应为以下类型:
INTEGER PRIMARY KEY AUTOINCRMENT'
尝试更改创建表 SQL,为您的 bug_id 字段提供如下别名:
而不是:
whats the XML of your list view rows look like? have you set the ID correctly?
EDIT:
Since your XML file seems to be set up correctly, I now believe your problem is something else. It could be that the data for the bug_id field is not being stored or retrieved properly from your database.
This is from android docs:
'Be sure to include an integer column named "_id" (with the constant _ID) for the IDs of the records. You should have this field whether or not you have another field (such as a URL) that is also unique among all records. If you're using the SQLite database, the _ID field should be the following type:
INTEGER PRIMARY KEY AUTOINCREMENT'
Try to change your create table SQL to give your bug_id field an alias like this:
as opposed to:
只需从此代码获取 ID 即可将此代码粘贴到您的 Database.java 文件中 -
在您的主 java 文件中使用如下 -
Simply get the ID's from this code & paste this code to your Database.java file -
In your main java file use like this -
发现问题:
布局宽度只有 5px,当我将文本填充 3px 时,它可能会消失在另一个布局下面......天哪
Found the issue:
Layout width was only 5px, when I padded text by 3px it probably dissapeared below another layout... geez