title: Adding table rows to a table layout in a loop
body: <p>I have written this code which tries to add Table Rows (which contain Text Views) inside a Table Layout. This is done in a loop. I get this error:</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testant/com.test.testant.products}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  </code></pre>
  
  <p>Can I somehow override the removeView() on parent? If I can't how can the removeView() be incorporated inside my code?</p>
  
  <p><strong>products.java</strong></p>
  
  <pre><code>public class products extends Activity{
  private DataBaseManager dataBase;
  
  //put the table name and column in constants
  public static final String TABLE_NAME_PRODUCTS = "products";
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.products);
      TableLayout tl = (TableLayout) findViewById(R.id.prodTable);
      TableRow tr = (TableRow) findViewById(R.id.prodRow);
  
  
  
      //creates and open the database so we can use it
      dataBase = DataBaseManager.instance();
  
      Cursor cursor = dataBase.select("SELECT * FROM " + TABLE_NAME_PRODUCTS);
      while (cursor.moveToNext()){
  
          tl.addView(tr);
  
  
      }
  
  }
  </code></pre>
  
  <p><strong>products.xml</strong></p>
  
  <pre><code>&lt;TableLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          android:id="@+id/prodTable"&gt;
  
          &lt;TableRow
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:clickable="false"
              android:id="@+id/prodRow"&gt;
  
              &lt;TextView
                  android:layout_width="0dp"
                  android:layout_height="fill_parent"
                  android:layout_weight=".08"
                  android:textColor="#fff"
                  android:id="@+id/prodCodeView"
                  android:clickable="true" /&gt;
              &lt;TextView
                  android:layout_width="0dp"
                  android:layout_height="fill_parent"
                  android:layout_weight=".30"
                  android:textColor="#fff"
                  android:id="@+id/prodNameView"
                  android:clickable="true" /&gt;
              &lt;TextView
                  android:layout_width="0dp"
                  android:layout_height="fill_parent"
                  android:layout_weight=".05"
                  android:textColor="#fff"
                  android:id="@+id/prodMUView" /&gt;
              &lt;TextView
                  android:layout_width="0dp"
                  android:layout_height="fill_parent"
                  android:layout_weight=".10"
                  android:textColor="#fff"
                  android:id="@+id/prodPriceView" /&gt;
              &lt;TextView
                  android:layout_width="0dp"
                  android:layout_height="fill_parent"
                  android:layout_weight=".05"
                  android:textColor="#fff"
                  android:id="@+id/prodVATView" /&gt;
              &lt;TextView
                  android:layout_width="0dp"
                  android:layout_height="fill_parent"
                  android:layout_weight=".05"
                  android:textColor="#fff"
                  android:id="@+id/prodIDView" /&gt;
          &lt;/TableRow&gt;
      &lt;/TableLayout&gt;
  </code></pre>