2013-10-25 62 views
0

在我的android應用程序中,我需要在運行時加載TableLayout。我已經實現了一種從SQLite數據庫檢索數據的方法,該數據庫返回一個ArrayList。我想要做的是如何在運行時在TableLayout中加載這些檢索到的數據?我需要爲它編寫一個單獨的方法。在我的xml文件中,我已經定義瞭如下的TableLayout。我需要在這個TableLayout加載表格數據。如果有人能夠如此友好地解釋我該如何做到這一點,我會非常感激。Android:在運行時加載表數據

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:scrollbarSize="5dp" 
    android:scrollbars="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <HorizontalScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <TableLayout 
       android:id="@+id/tblPersons" 
       android:layout_width="fill_parent" 
       android:layout_height="match_parent" > 
      </TableLayout> 
     </HorizontalScrollView> 
    </LinearLayout> 
</ScrollView> 

提前感謝名單

+1

雖然這當然是可以的,但請考慮使用ListView來顯示數據列表。 ListViews是爲了這樣的事情。 –

+0

'foreach elem in array {inflate tablerow;用elem中的數據設置表格字段; tblPersons.addView(tablerow的); }但像Ken寫ListView是更好的選擇 – Selvin

回答

0

你需要做的讀書有點

ListView

Custom ListView Adapter

基本思想是:

1)你已經有了佈局XML,使其兼容爲ListView [那是你不需要的表格佈局]

2)做一個ListView和編寫自定義適配器類是

3)中,你充氣你的XML佈局到ListView作爲選項ListViewItem