Java 将表行添加到循环中的表布局
我编写了这段代码,试图在表布局中添加表行(包含文本视图)。这是在循环中完成的。我得到这个错误:Java 将表行添加到循环中的表布局,java,android,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 r
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>
您必须动态创建
表格行
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
。必须动态创建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);
}
您不能添加多个相同的
表行。您正试图一次又一次地将相同的表行添加到一个表中。导致异常的原因是:
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);
}
希望能有所帮助。您正在尝试一次又一次地将同一个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);
}
希望有帮助。您的每一行都需要是自己的实例。您在上面实现它的方式是一样的。这是行不通的。
您需要使用布局充气器充气循环中的表行,或手动创建对象
使用充气机可能如下所示:
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);
}
您的每一行都需要是它们自己的实例。您在上面实现它的方式是一样的。这是行不通的。
您需要使用布局充气器充气循环中的表行,或手动创建对象
使用充气机可能如下所示:
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);
}
findViewByID()不创建新对象,它只返回已经存在的对象-对同一实例的另一个引用,这不会对情况有太大的改善,在这里。没有注意到他在复制时使用了findViewById。谢谢:)现在它可以工作了,但是新的表看起来不像products.xml中的表。我希望它包含这些文本视图并具有相同的布局。@user2315641您也可以通过编程将文本视图放入TableRow中。这超出了整个过程的全部目的。我希望每个周期中创建的包含文本视图的行由数据库中的数据填充。基本上,在products中创建行是没有意义的。xml.findviewbyd()不创建新对象,它只返回已经存在的内容-对同一实例的另一个引用,这不会对情况有太大的改善。没有注意到他在复制时使用了findViewById。谢谢:)现在它可以工作了,但是新的表看起来不像products.xml中的表。我希望它包含这些文本视图并具有相同的布局。@user2315641您也可以通过编程将文本视图放入TableRow中。这超出了整个过程的全部目的。我希望每个周期中创建的包含文本视图的行由数据库中的数据填充。基本上,在products.xml中创建行是没有意义的。我是这样使用它的吗:TableRow.LayoutParams(R.id.prodRow,AttributeSet attrs)?我该如何设置属性集属性?它还会添加包含的文本视图吗?我是否会这样使用:TableRow.LayoutParams(R.id.prodRow,AttributeSet attrs)?我该如何设置属性集属性?它还会添加包含的文本视图吗?我自己已经取得了一些进展,但这个答案填补了我的一些空白。非常感谢。我自己已经取得了一些进步,但这个答案填补了我的一些空白。非常感谢。