Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding and removing table rows - Android

Tags:

android

I am trying to dynamically add and remove rows from a TableLayout.

The layout is defined in an xml file.

I am able to successfully remove a row, but when I call the corresponding addView command nothing happens.

table = (TableLayout)findViewById(R.id.table);
row = (TableRow)findViewById(R.id.row);

table.removeView(row);

table.addView(row);

This results in a row being removed, but not being added again.

Edit: It turns out it was adding if after all, just at the bottom of the screen instead of in the same location as it was removed from.

I am able to add it in the correct position by specifying the index:

table.addView(row,4); // 4 happens to the the row

but I can not figure out how to determine the index of the row , there does not seem to be a method to accomplish this. anyone know how do to that? (ie. if I did not know the index was 4 how could I figure that out)

Edit: included XML. this is just the row in question, there are other rows above and below it

<TableRow android:id="@+id/row">

        <TextView android:id="@+id/field1"
            android:text="testing"
        android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dip"
            android:textStyle="bold"
            android:textSize="18dip"
        />


        <TextView android:id="@+id/field2"
            android:padding="3dip"
            android:text="test"
            android:textSize="18dip"
            android:gravity="right"
        />



    </TableRow>
like image 739
brodie31k Avatar asked Sep 10 '09 19:09

brodie31k


1 Answers

Use hierarchyviewer (in your SDK tools/ directory) to determine if the row is truly not being added, or is being added but some layout parameters are messed up and so it is not appearing on-screen.

like image 180
CommonsWare Avatar answered Sep 21 '22 18:09

CommonsWare