我想在运行时向TableLayout添加行,但数据根本没有显示出来。动态添加行到TableLayout没有显示
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarSize="10dp"
android:scrollbars="horizontal|vertical" >
<TableLayout
android:id="@+id/server_dataTable"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/color_data_table_header_background"
android:stretchColumns="*" >
</TableLayout>
</ScrollView>
</LinearLayout>
相关的代码:
private TableLayout addRowToTable(TableLayout table, String[] data) {
Context context = this.getApplicationContext();
TableRow row = new TableRow(context);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams();
// Wrap-up the content of the row
rowParams.height = TableLayout.LayoutParams.WRAP_CONTENT;
rowParams.width = TableLayout.LayoutParams.WRAP_CONTENT;
for (int i = 0; i < data.length; i++) {
TableRow.LayoutParams columnParams = new TableRow.LayoutParams();
// wrap-up content of the row
columnParams.height = TableRow.LayoutParams.WRAP_CONTENT;
columnParams.width = TableRow.LayoutParams.WRAP_CONTENT;
// set gravity to center of the column
columnParams.gravity = Gravity.CENTER;
TextView column = new TextView(context);
column.setText(data[i]);
row.addView(column, columnParams);
}
table.addView(row, rowParams);
return table;
}
我打电话从我活动的onCreate()addRowToTable
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);
// Show the Up button in the action bar.
setupActionBar();
TableLayout dataTableHeader = (TableLayout) findViewById(R.id.server_dataTable);
dataTableHeader = addRowToTable(dataTableHeader, headerLabels);
}
我已经在这了现在大约40分钟,已经阅读了几乎所有关于StackOverflow的相关问题,但都没有找到任何有用的。任何帮助/指针都会很棒!
编辑:添加了活动的onCreate()。
你的'addRowToTable'运行在UI线程上吗? –
是啊!我应该线程吗? –
发布您的所有代码 –