Afa
Afa

Reputation: 81

dynamically added rows are not shown in android

Please help. When i debug app, rows are added but table is not displayed on screen i have created separately xml for tablerow and tablelayout is in main xml file

    table = (TableLayout) findViewById(R.id.goodsTable);

     for (int i = 0; i < attrName.length; i++) {
                            TableRow row = (TableRow) LayoutInflater.from(getApplicationContext()).inflate(R.layout.attribute_row, null);
                            ((TextView) row.findViewById(R.id.attributeName)).setText(attrName[i]);
                            ((TextView) row.findViewById(R.id.attributeValue)).setText(barcode);
                            table.addView(row);
                        }

attribute_row xml :

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/attributeName"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:textStyle="bold"
        />

    <TextView
        android:id="@+id/attributeValue"
        android:layout_height="wrap_content"
        android:gravity="right" />
</TableRow>

main xml file:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:orientation="horizontal"
        android:padding="10dp">
        <EditText
            android:id="@+id/goodsBarcode"
            android:layout_width="262dp"
            android:layout_height="wrap_content"
            android:inputType="textCapWords"
            android:padding="10dp"
            android:singleLine="false"
            android:background="@android:color/transparent" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="search"
            android:id="@+id/search"
             />
        </LinearLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TableLayout
                android:id="@+id/goodsTable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </TableLayout>
        </LinearLayout>
    </ScrollView>


</LinearLayout>

please help; when i debug app, rows are added but table is not displayed on screen i have created separately xml for tablerow and tablelayout is in main xml file

Upvotes: 2

Views: 104

Answers (4)

Afa
Afa

Reputation: 81

after struggling with my code for two days, finally solved issue, my mistake was to use "ApplicationContext" instead of "Activity Context" ; i need to use TableRow row = (TableRow) Inflater.from(this).inflate(R.layout.attribute_row, table, false); instead of TableRow row = (TableRow) LayoutInflater.from(getApplicationContext()).inflate(R.layout.attribute_row, table, false);

Upvotes: 0

Viral Patel
Viral Patel

Reputation: 33408

After adding all rows you want just call this method on the table object to have the newly added rows reflect on the UI:

table.requestLayout();

I have a similar code and works fine by adding the requestLayout() method.

My dynamic table in layout xml file looks like this:

<TableLayout android:id="@+id/myTable"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="5dp">

    <!--Dynamic content here-->

</TableLayout>

Note: android:layout_width for TableLayout and any other View containing it should be match_parent. Because wrap_content will cause resize issues when you add new rows.

Upvotes: 1

Juan Cruz Soler
Juan Cruz Soler

Reputation: 8254

Do not pass null as the parent when calling inflate(...) because it can't determine the attributes that are referencing the parent (e.g android:layout_width, android:layout_height).

Do instead:

TableRow row = (TableRow) LayoutInflater.from(getApplicationContext()).inflate(R.layout.attribute_row, table, false);

Upvotes: 1

Meenaxi
Meenaxi

Reputation: 567

try to set table.addView(row,i);

or you can create and add row dynamically.

 TableRow row;

    for (int i = 0; i <=lstfDue.size(); i++)
    {
        row= new TableRow(this);
        row.setLayoutParams(rowParams);

        txtDate = new TextView(this);
        txtAmount = new TextView(this);

        txtDate.setText("text here");
        txtAmount.setText("text here");

        row.addView(txtDate);
        row.addView(txtFeeType);
        row.addView(txtAmount);
        tblFeesDue.addView(row, i);
    }

Upvotes: 0

Related Questions