Abhishek Bhardwaj
Abhishek Bhardwaj

Reputation: 103

Dynamically added rows to TableLayout not showing up

I'm trying to add rows to TableLayout at runtime but the data is simply not showing up.

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>

The relevant code:

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;
}

I'm calling addRowToTable from my activity's onCreate():

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);
}

I've been at this for about 40 minutes now and have already read almost all related questions on StackOverflow but haven't found anything helpful. Any help/pointers would be great!

Edit: Added the activity's onCreate().

Upvotes: 0

Views: 1153

Answers (1)

M D
M D

Reputation: 47817

Passed Activity context into your addRowToTable method like

dataTableHeader = addRowToTable(youractivity.this,dataTableHeader, headerLabels);

and Access into addRowToTable like:

private TableLayout addRowToTable(Activity a,TableLayout table, String[] data) {
TableRow row = new TableRow(a);
........
........
table.addView(row);

return table;
}

Try this way and give me feedback

Update: try out this way:

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;
}

Upvotes: 1

Related Questions