我正在尝试在运行时向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()。
答案 0 :(得分:1)
将Activity
上下文传递到您的addRowToTable
方法,如
dataTableHeader = addRowToTable(youractivity.this,dataTableHeader, headerLabels);
并访问addRowToTable
,如:
private TableLayout addRowToTable(Activity a,TableLayout table, String[] data) {
TableRow row = new TableRow(a);
........
........
table.addView(row);
return table;
}
尝试这种方式并给我反馈
更新:尝试这种方式:
private TableLayout addRowToTable(Activity a,TableLayout table, String[] data) {
TableRow row = new TableRow(a);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
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(a);
column.setText(data[i]);
row.addView(column,columnParams);
}
table.addView(row);
return table;
}