Dynamically added table rows not appearing
Asked Answered
P

2

6

I have seen many posts regarding dynamically adding table rows, but I am not sure what I'm missing.

When I execute the following, nothing is displayed (aside from application title bar).

My Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        android:id="@+id/table_view_test_main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        >
    <ScrollView
            android:id="@+id/tvt_scroll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            >
        <RelativeLayout
                android:id="@+id/tvt_scroll_relative"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <TableLayout
                    android:id="@+id/tvt_tableview"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    >
            </TableLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

My Activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_view);

    TableLayout tableLayout = (TableLayout) findViewById(R.id.tvt_tableview);

    TableRow tableRow = new TableRow(this);
    tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

    TextView column1 = new TextView(this);
    column1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    column1.setBackgroundColor(Color.YELLOW);
    column1.setTextColor(Color.BLUE);
    column1.setText("Col1 Value");
    tableRow.addView(column1);

    TextView column2 = new TextView(this);
    column2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    column2.setBackgroundColor(Color.RED);
    column2.setTextColor(Color.GREEN);
    column2.setText("Col2 Value");
    tableRow.addView(column2);

    tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

    //tl.refreshDrawableState();
    //findViewById(R.id.tvt_scroll_relative).refreshDrawableState();
}
Paddlefish answered 24/9, 2011 at 20:36 Comment(3)
Interesting. This all looks good to me (not that I am absolutely sure of anything here...). I will recommend trying to dynamically add other TextView's to the other layouts above the TableLayout. See if you can get something to show up. Let us know how it works out.Matroclinous
remove the line tableRow.setLayoutParams(); after you have instantiated TableRow, it should work i think. Let me know if this works, so that i can post it as a solution.Solangesolano
@Yashwanth Kumar, removing that alone did not help. Changing ViewGroup.LayoutParams to TableRow.LayoutParams or removing them altogether does resolve the issue.Paddlefish
E
13

Change the two references

ViewGroup.LayoutParams

to

TableRow.LayoutParams

and it should work.

Adding layout params to a widget requires that the LayoutParams are a inner class of the surrounding layout container.

Exarate answered 24/9, 2011 at 21:1 Comment(2)
Thanks for the answer, do you have a reference for using the inner class of the surrounding layout container?Paddlefish
I had the same problem and discovered it after a some experiencing. I would be interested in some reference, too.Exarate
C
1

I think this has something to do with you setting the layout params to the TextViews. If you remove those definitions the info appears. Also if you take a look at the official documentation for TableRow you can see:

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

Cirrate answered 24/9, 2011 at 21:5 Comment(1)
Not entirely true, as you can set TableRow.LayoutParams.span, in which case you will need to set the LayoutParams.Paddlefish

© 2022 - 2024 — McMap. All rights reserved.