gpt4 book ai didi

java - 以循环方式将表行添加到表布局

转载 作者:行者123 更新时间:2023-12-01 18:26:14 25 4
gpt4 key购买 nike

我编写了这段代码,尝试在表布局内添加表行(包含 TextView )。这是循环完成的。我收到此错误:

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()合并到我的代码中?

产品.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);


}

}

产品.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>

最佳答案

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

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

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

代码 fragment :

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

希望有帮助。

关于java - 以循环方式将表行添加到表布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26095104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com