0
所以我有从我的SQLite数据库中提取数据的问题。Android SQLite通过光标在表格中显示数据
这是我的光标,它从db获取数据,它在DatabaseAdapter类中。
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;
}
然后我建立一个表,我打电话给我的光标
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
嗯......你打电话几次movetofirst。你有多少数据? –
什么意思是nothign发生?没有数据被引入? –
我有大约6栏,没有任何显示 – Infinitude