0

我已经用谷歌搜索并搜索了这个网站的答案,但什么也没得到;/所以,这是我的问题,我有 listview,我尝试用数据库中的数据填充它,但列表什么也没显示。我真的坚持这个,这是我的代码:

DatabaseViewerActivity.java:

protected void onCreate(Bundle savedInstaceState)
{
    super.onCreate(savedInstaceState);
    setContentView(R.layout.database_layout);

    SQLiteDatabase database = openOrCreateDatabase("qr_scanner.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);


    String q = "SELECT id as _id,serial,desc FROM inv_urzadzenia;";
    Cursor c = database.rawQuery(q, null);

    startManagingCursor(c);
    ListAdapter adapter = new SimpleCursorAdapter(this,R.layout.single_row_item,c,new String[]{"serial","desc"},new int[]{R.id.deviceSerial,R.id.deviceDesc});
    ListView lv = (ListView)findViewById(R.id.deviceList);

    lv.setAdapter(adapter);
    lv.setVisibility(View.VISIBLE);
    c.close();
    database.close();
}

这是我的 single_row_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayoutHeader"
android:layout_height="wrap_content"  android:layout_width="fill_parent">
    <TextView
    android:id="@+id/deviceSerial"
    android:layout_width="wrap_content"
    android:layout_height="?android:attr/listPreferredItemHeight" android:layout_alignParentLeft="true"></TextView>
<TextView
    android:id="@+id/deviceDesc"
    android:layout_width="wrap_content"
    android:layout_height="?android:attr/listPreferredItemHeight"  android:layout_alignParentRight="true"></TextView>
</RelativeLayout>

和 database_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="11pt"
    android:text="Urzadzenia"/>
    <RelativeLayout
    android:id="@+id/RelativeLayoutHeader"
    android:layout_height="wrap_content"  android:layout_width="fill_parent">
            <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:textColor="#f00"
            android:text="String" android:layout_alignParentLeft="true" ></TextView>
        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:textColor="#f00"
            android:text="String" android:layout_alignParentRight="true"></TextView>
</RelativeLayout>
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/deviceList" android:divider="#000" android:isScrollContainer="true" android:headerDividersEnabled="true" android:scrollbarAlwaysDrawVerticalTrack="true" android:scrollbars="vertical"/>

</LinearLayout>

我不知道问题出在哪里,我检查了 c.getCount() 和 adapter.getCount() 不返回 0,

我的日志猫:

09-20 07:23:43.574: W/KeyCharacterMap(8915): No keyboard for id 0 09-20 07:23:43.574: W/KeyCharacterMap(8915): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
09-20      07:28:37.994: W/ActivityManager(67): Activity pause timeout for HistoryRecord{43fd4b50 com.android.launcher/com.android.launcher2.Launcher}

当我从 statemet 中删除“AS _id”时出现的异常

09-20 07:32:36.644: E/AndroidRuntime(16725): java.lang.RuntimeException: Unable to start activity ComponentInfo{projects.qrscanner/projects.qrscanner.DatabaseViewerActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
4

1 回答 1

1

由于您使用的是startManagingCursor(),您可以删除代码 c.close(); 数据库.close(); 因为android框架会在活动关闭时关闭游标和数据库。还有线条

    c.close();
    database.close();

在您的代码中创建问题是您正在从此游标创建一个适配器对象,您最终会强制关闭该对象,因此适配器支持的数据集会失效,因此您看不到任何结果。

这是可以工作的示例,希望对您有所帮助。

    SQLiteDatabase database = openOrCreateDatabase("testdb.db",
                SQLiteDatabase.CREATE_IF_NECESSARY, null);

        String createTable = "CREATE TABLE IF NOT EXISTS " + "testtable" + "(" + "_id"
                + " integer primary key autoincrement, " + "serial1"
                + " text not null, " + "desc1" + " text not null, "
                + "id text);";

        String q = "SELECT id as _id,serial1,desc1 FROM testtable;";
        database.execSQL(createTable);
        for (int i = 0; i < 20; i++) {
            String serial = "serial - "+i;
            String desc = "desc - "+i;
            String id = "id - "+i;
            String sql =
                    "INSERT or replace INTO testtable (serial1, desc1, id)       VALUES('"+serial+"'"+",'"+desc+"'"+",'"+id+"')" ;       
                        database.execSQL(sql);
        }
        Cursor c = database.rawQuery(q, null);

        startManagingCursor(c);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.single_row_item_layout, c, new String[] { "serial1",
                        "desc1" }, new int[] { R.id.deviceSerial,
                        R.id.deviceDesc });
        ListView lv = (ListView) findViewById(R.id.deviceList);

        lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        lv.setVisibility(View.VISIBLE);
于 2013-09-20T09:31:48.397 回答