[英]Adding table rows to a table layout in a loop
我編寫了這段代碼,試圖在表格布局內添加表格行(其中包含文本視圖)。 這是循環完成的。 我收到此錯誤:
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
添加多個。
您的每一行都必須是各自的實例。 上面您實現它的方式是相同的。 那行不通。 您需要使用layout-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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.