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) ";
4

4 回答 4

0

您的“id-field”确实具有名称“_id”,这一点非常重要,否则它可能不起作用。

于 2011-12-16T20:20:52.147 回答
0

您的列表视图行的 XML 是什么样的?您是否正确设置了 ID?

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

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

整数主键自动增量'

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

bug_id as _id integer primary key autoincrement,

相对于:

bug_id integer primary key autoincrement,
于 2011-12-16T20:21:50.197 回答
0

只需从此代码中获取 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});
于 2011-12-19T13:41:40.120 回答
0

发现问题:

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

布局宽度只有 5 像素,当我将文本填充 3 像素时,它可能在另一个布局下方消失了……天哪

于 2011-12-19T15:25:34.660 回答