Q:

Adding table rows to a table layout in a loop

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:
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.

Can I somehow override the removeView() on parent? If I can't how can the removeView() be incorporated inside my code?
products.java
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);

    }

}

products.xml
<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/prodTable">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="false"
            android:id="@+id/prodRow">

            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".08"
                android:textColor="#fff"
                android:id="@+id/prodCodeView"
                android:clickable="true" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".30"
                android:textColor="#fff"
                android:id="@+id/prodNameView"
                android:clickable="true" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".05"
                android:textColor="#fff"
                android:id="@+id/prodMUView" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".10"
                android:textColor="#fff"
                android:id="@+id/prodPriceView" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".05"
                android:textColor="#fff"
                android:id="@+id/prodVATView" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".05"
                android:textColor="#fff"
                android:id="@+id/prodIDView" />
        </TableRow>
    </TableLayout>

A:

You are trying to add the same TableRow again and again a Table. Which is causing exception:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

You should create a new TableRow in a loop itself
Code snippet:
Cursor cursor = dataBase.select("SELECT * FROM " + TABLE_NAME_PRODUCTS);
while (cursor.moveToNext()){
    TableRow tr = new TableRow(this);
    //set width and height using layout params
    tl.addView(tr);
}

Hope it helps.

