2010-07-16 18 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参考,这将帮助。