2015-01-17 44 views
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

+0

嗯......你打電話幾次movetofirst。你有多少數據? –

+0

什麼意思是nothign發生?沒有數據被引入? –

+0

我有大約6欄,沒有任何顯示 – Infinitude

回答

相關問題