2010-07-16 19 views
2

我有一個奇怪的問題,從碼代替錶行。Android的改變tableLayout從代碼不起作用

我的XML代碼如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 

<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/columnheadings" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent"/>   

<ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:dividerHeight="2px" 
    /> 
    ...  
</LinearLayout> 

在創建活動中,我可以成功插入的TableRow到TableLayout 「列標題」:

table = (TableLayout)findViewById(R.id.columnheadings); 
table.removeAllViews(); 
TableRow tr = new TableRow(this); 
TextView tv = new TextView(activity); 
tv.setText("asdf"); 
tr.addView(tv); 
table.addView(tr); 
table.postInvalidate(); 

內容(以及寬度)是動態計算的。

現在我想重繪TableLayout,當ListView的適配器中有更新時。爲此,我將創建適配器並引用該活動。

從適配器中我可以成功地改變例如一列的文字。

我嘗試做(沒有成功):

TableLayout tableLayout = (TableLayout)activity.findViewById(android.app.R.id.columnheadings);   

tableLayout.removeAllViews(); 
TableRow row=new TableRow(activity); 
TextView tv = new TextView(activity); 
tv.setText("test"); 

row.addView(tv); 
tableLayout.addView(row); 

tableLayout.postInvalidate(); 

不幸的是,這種情況發生的唯一事情就是移除當前錶行。新行不會從適配器添加到tableLayout中。

有什麼建議嗎?

回答

0

我不記得爲什麼,但我的代碼有row.getParent().requestLayout();在所有操作結束。似乎沒有它也不適合我。我不需要在我的代碼,這就是爲什麼我用row.getParent(),我覺得你可以簡單地寫tableLayout.requestLayout();TableLayout參考,這將幫助。