2014-09-29 216 views
0

我已經編寫了此代碼,它試圖在表格佈局內添加表格行(其中包含文本視圖)。這是在一個循環中完成的。我得到這個錯誤:將表格行添加到循環中的表格佈局

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> 

回答

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

希望它能幫助。

+0

findViewByID()不會創建一個新的對象,它只返回已經存在的對象 - 對同一個實例的另一個引用,在這裏不會改善情況。 – Mjoellnir 2014-09-29 08:11:00

+0

沒錯。錯過了他在複製時使用了findViewById。謝謝:) – 2014-09-29 08:12:16

+0

不客氣:) – Mjoellnir 2014-09-29 08:13:06

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不止一個。

+0

難道我這樣使用它:TableRow.LayoutParams(R.id.prodRow,AttributeSet中的ATTRS)?我對AttributeSet attrs有什麼建議?它會添加包含的TextViews嗎? – user2315641 2014-09-29 08:27:15

+0

爲什麼要投票? – Rustam 2014-09-29 18:30:48

1

您的每行都需要是它們自己的實例。你在上面實現的方式,它們只是一個一樣的。這是行不通的。 您需要使用佈局充氣器來膨脹循環中的表格行或手動創建對象。

使用吹氣看起來是這樣的:

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

我已經取得了一些進展,但這個答案填補了我的一些空白。謝謝! – user2315641 2014-09-29 09:53:07

+0

不客氣。 :) – Mjoellnir 2014-09-29 10:22:19