Android SQLite 在表行中按游标显示数据

Android SQLite showing data by cursor in tablerows

所以我在从我的 SQLite 数据库中提取数据时遇到了问题。

这是我的游标,它从数据库中获取数据,它位于 DatabaseAdapter class。

    public Cursor getAllData2() {

        String[] allColumns = new String[] {DatabaseAdapter.UID, DatabaseAdapter.WIEK, DatabaseAdapter.WZROST, DatabaseAdapter.WAGA, DatabaseAdapter.BMI, DatabaseAdapter.DATA};

        Cursor cursor = db.query(DatabaseAdapter.TABLE_NAME, allColumns, null, null, null, null, null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

然后我正在构建一个 table,我在其中调用我的光标

    private void BuildTable() {

    Cursor cursor = bazadanych.getAllData2();

    int rows = cursor.getCount();
    int cols = cursor.getColumnCount();

    cursor.moveToFirst();
    for (int i = 0; i < rows; i++) {
        TableRow row = new TableRow(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            for (int j = 0; j < cols; j++)  {
                TextView tv = new TextView(this);
                tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                tv.setGravity(Gravity.CENTER);
                tv.setTextSize(16);
                tv.setPadding(0, 5, 0, 5);

                tv.setText(cursor.getString(j));

                row.addView(tv);

            }
        cursor.moveToNext();
        tabela.addView(row);

    }

}

然后我调用 BuildTable();点击按钮后。 什么也没发生。

XML 文件。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/wczytaj"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="Button" />

<TableLayout
    android:id="@+id/tabela"
    android:layout_width="fill_parent"
    android:layout_height="382dp"
    android:layout_marginTop="63dp"
    android:layout_weight="2.02"
    android:padding="10dp"
    android:shrinkColumns="*"
    android:stretchColumns="*" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dip" >

        <TextView
            android:text="ID" />

        <TextView
            android:text="Wiek" />

        <TextView
            android:text="Wzrost" />

        <TextView
            android:text="Waga" />

        <TextView
            android:text="BMI" />

        <TextView
            android:text="Data" />
    </TableRow>
</TableLayout>

当我删除所有 TableRow 时,错误除以 0。

需要更改此行:

row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

进入这个:

tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

https://code.google.com/p/android/issues/detail?id=19343答案在这里