将表行添加到循环中的表布局

时间:2014-09-29 07:59:26

标签: java android

我编写了这段代码,试图在表格布局中添加表格行(包含文本视图)。这是在一个循环中完成的。我收到这个错误:

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.

我可以以某种方式覆盖父级的removeView()吗?如果我不能如何将removeView()合并到我的代码中?

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>

3 个答案:

答案 0 :(得分:2)

您正在尝试一次又一次地添加相同的TableRow表。这导致异常:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

您应该在循环中创建一个新的TableRow

代码段:

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);
}

希望它有所帮助。

答案 1 :(得分:1)

您必须动态创建TableRow

Cursor cursor = dataBase.select("SELECT * FROM " + TABLE_NAME_PRODUCTS);
while (cursor.moveToNext()){
  TableRow tr=new TableRow(context); // either create it here or get reference from xml file.
       // setting layoutparam to tr and then add it to table layout tl;
    tl.addView(tr);


}

您不能添加多个TableRow

答案 2 :(得分:1)

您的每一行都需要是自己的实例。你在上面实现它的方式,它们只是一个相同。那不行。 您需要使用layout-inflater对循环中的表行进行充气,或者手动创建对象。

使用inflater可能如下所示:

Cursor cursor = dataBase.select("SELECT * FROM " + TABLE_NAME_PRODUCTS);
while (cursor.moveToNext()){
    TableRow tr = (TableRow) View.inflate(this, R.id.prodRow, tl);
    // now get the cells from the row by findViewByID()
    // and fill them with your data...
    // This addView might be redundant. If you have your rows twice in your table
    // just remove the addView() line, as it might be done by the inflate() method,
    // I am not sure of that by heart.
    tl.addView(tr);
}