如何从表中获取_id字段并将其放入ListView

发布于 2024-12-22 02:50:58 字数 2023 浏览 1 评论 0原文

我在这里有点困惑。

    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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏雪无痕 2024-12-29 02:50:58

您的“id-field”的名称必须是“_id”,这一点非常重要,否则它可能无法工作。

It's very important that your "id-field" does have the name "_id", else it might not work.

太傻旳人生 2024-12-29 02:50:58

列表视图行的 XML 是什么样的?你设置的ID正确吗?

编辑:
由于您的 XML 文件似乎设置正确,我现在相信您的问题是其他问题。可能是 bug_id 字段的数据未从数据库中正确存储或检索。

这是来自 android 文档:
'请确保包含一个名为“_id”的整数列(带有常量 _ID)作为记录的 ID。无论您是否有其他在所有记录中也是唯一的字段(例如 URL),您都应该拥有此字段。如果您使用的是 SQLite 数据库,则 _ID 字段应为以下类型:

INTEGER PRIMARY KEY AUTOINCRMENT'

尝试更改创建表 SQL,为您的 bug_id 字段提供如下别名:

bug_id as _id integer primary key autoincrement,

而不是:

bug_id integer primary key autoincrement,

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:

bug_id as _id integer primary key autoincrement,

as opposed to:

bug_id integer primary key autoincrement,
落在眉间の轻吻 2024-12-29 02:50:58

只需从此代码获取 ID 即可将此代码粘贴到您的 Database.java 文件中 -

String[] return_result()
{

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(tableName, new String[] { "_id", "date", "description"},null, null, null, null, null);
    String[] result = new String[cursor.getCount()];
    int i = 0;
    if(cursor.moveToFirst())
    {
        do
        {
            result[i]= cursor.getString(cursor.getColumnIndex("_id"));
            i++;
        }while(cursor.moveToNext());
    }
    if (cursor!=null && !cursor.isClosed()) 
    {
        cursor.close();
        db.close();
    }
    return result;
}

在您的主 java 文件中使用如下 -

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.bugs_list_item, itemCursor, 
        new String[] {db.return_result(), db.BUGS_DATE, db.BUGS_DESCRIPTION}, 
        new int[] {R.id.bug_id, R.id.bug_date, R.id.bug_description});

Simply get the ID's from this code & paste this code to your Database.java file -

String[] return_result()
{

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(tableName, new String[] { "_id", "date", "description"},null, null, null, null, null);
    String[] result = new String[cursor.getCount()];
    int i = 0;
    if(cursor.moveToFirst())
    {
        do
        {
            result[i]= cursor.getString(cursor.getColumnIndex("_id"));
            i++;
        }while(cursor.moveToNext());
    }
    if (cursor!=null && !cursor.isClosed()) 
    {
        cursor.close();
        db.close();
    }
    return result;
}

In your main java file use like this -

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.bugs_list_item, itemCursor, 
        new String[] {db.return_result(), db.BUGS_DATE, db.BUGS_DESCRIPTION}, 
        new int[] {R.id.bug_id, R.id.bug_date, R.id.bug_description});
柠檬心 2024-12-29 02:50:58

发现问题:

    android:layout_width="5dip"             
    android:padding="3dip" 

布局宽度只有 5px,当我将文本填充 3px 时,它可能会消失在另一个布局下面......天哪

Found the issue:

    android:layout_width="5dip"             
    android:padding="3dip" 

Layout width was only 5px, when I padded text by 3px it probably dissapeared below another layout... geez

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文