将表格行添加到循环中的表格布局
我已经编写了此代码,它试图在表格布局内添加表格行(其中包含文本视图)。这是在一个循环中完成的。我得到这个错误:将表格行添加到循环中的表格布局
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>
您正试图添加相同的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);
}
希望它能帮助。
你必须创建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
不止一个。
难道我这样使用它:TableRow.LayoutParams(R.id.prodRow,AttributeSet中的ATTRS)?我对AttributeSet attrs有什么建议?它会添加包含的TextViews吗? – user2315641 2014-09-29 08:27:15
为什么要投票? – Rustam 2014-09-29 18:30:48
您的每行都需要是它们自己的实例。你在上面实现的方式,它们只是一个一样的。这是行不通的。 您需要使用布局充气器来膨胀循环中的表格行或手动创建对象。
使用吹气看起来是这样的:
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);
}
我已经取得了一些进展,但这个答案填补了我的一些空白。谢谢! – user2315641 2014-09-29 09:53:07
不客气。 :) – Mjoellnir 2014-09-29 10:22:19
findViewByID()不会创建一个新的对象,它只返回已经存在的对象 - 对同一个实例的另一个引用,在这里不会改善情况。 – Mjoellnir 2014-09-29 08:11:00
没错。错过了他在复制时使用了findViewById。谢谢:) – 2014-09-29 08:12:16
不客气:) – Mjoellnir 2014-09-29 08:13:06