title: Dynamically adding and removing table rows - Android
body: <p>I am trying to dynamically add and remove rows from a TableLayout.</p>
  
  <p>The layout is defined in an xml file.</p>
  
  <p>I am able to successfully remove a row, but when I call the corresponding addView command nothing happens.</p>
  
  <pre><code>table = (TableLayout)findViewById(R.id.table);
  row = (TableRow)findViewById(R.id.row);
  
  table.removeView(row);
  
  table.addView(row);
  </code></pre>
  
  <p>This results in a row being removed, but not being added again.</p>
  
  <p><strong>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.</strong></p>
  
  <p>I am able to add it in the correct position by specifying the index:</p>
  
  <pre><code>table.addView(row,4); // 4 happens to the the row
  </code></pre>
  
  <p>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)</p>
  
  <p>Edit: included XML. this is just the row in question, there are other rows above and below it</p>
  
  <pre><code>&lt;TableRow android:id="@+id/row"&gt;
  
          &lt;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"
          /&gt;
  
  
          &lt;TextView android:id="@+id/field2"
              android:padding="3dip"
              android:text="test"
              android:textSize="18dip"
              android:gravity="right"
          /&gt;
  
  
  
      &lt;/TableRow&gt;
  </code></pre>