8

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>
2
  • Your code works fine with a test layout. Like to see your XML layout to comment further. Commented Sep 11, 2009 at 6:26
  • Did you have rows above and below the row in question? Commented Sep 11, 2009 at 15:06

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't quite help here, but thanks for the heads up I hadn't taken a look at hierarchyviewer before.
1
public int indexOfChild (View child) 

public View getChildAt (int index) 

Both methods provided by TableLayout. ;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.