I got a strange problem with replacing a table row from code.
My XML code looks like this:
<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>
When the activity is created, I can successfully insert a TableRow to the TableLayout "columnheadings":
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();
The contents (and width) of the table columns are dynamically calculated.
Now I want to also redraw the TableLayout, when there are updates within the Adapter of the ListView. For this purpose, I'm creating the adapter with a reference to the activity.
From within the adapter I can successfully change e.g. the text of a column.
What I try to do (without success):
TableLayout tableLayout = (TableLayout)activity.fin开发者_开发百科dViewById(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();
Unfortunately, the only thing that happens is a removal of the current table row. The new row isn't added to the tableLayout from the adapter.
Any suggestions ?
I do not remember why, but my code has row.getParent().requestLayout();
at the end of all manipulations. Seems like it was not working for me without it too. I didn't need TableLayout
reference in my code that's why I used row.getParent()
, I think you can simply write tableLayout.requestLayout();
and it will help.
精彩评论