2014-04-19 62 views
0

我需要動態添加tablerows到我的表。在每一行中必須有4個TextView .. 當我運行程序時,出現錯誤「您必須先調用子視圖的父級removeView」。我該怎麼辦? 這是我的代碼和我的XML:調用第一個孩子的父母removeView

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

View rootView = inflater.inflate(R.layout.post_lineup_sending_layout, 
     container, false); 

    tableLayout =(TableLayout)rootView.findViewById(R.id.postsending_table_layout); 

    ownPlayer = (TextView) rootView.findViewById(R.id.own_player); 
    pointsOwnPlayer = (TextView) rootView.findViewById(R.id.own_player_points); 
    enemyPlayer = (TextView) rootView.findViewById(R.id.other_player); 
    pointsEnemyPlayer = (TextView) rootView.findViewById(R.id.other_player_points); 


    for(int i=0; i<4; i++){ 
     TableRow tr = new TableRow(CurrentContext.getContext()); 
     ownPlayer.setText("Ciccio"); 
     pointsOwnPlayer.setText(""+6); 
     enemyPlayer.setText("Avversario"); 
     pointsEnemyPlayer.setText(""+10); 
     tr.addView(ownPlayer); 
     tr.addView(pointsOwnPlayer); 
     tr.addView(enemyPlayer); 
     tr.addView(pointsEnemyPlayer); 
     tableLayout.addView(tr); 
    } 

    return rootView; 
} 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TableLayout 
     android:id="@+id/postsending_table_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TableRow > 

     <TextView 
      android:id="@+id/own_player" 
      android:text="CALCIATORE_0" 
      android:padding="3dip" 
      android:layout_width="125dp" /> 

     <TextView 
      android:id="@+id/own_player_points" 
      android:text="6" 
      android:gravity="center" 
      android:layout_width="30dp" /> 

     <TextView 
      android:id="@+id/other_player" 
      android:gravity="center" 
      android:text="CALCIATORE_1" 
      android:layout_width="125dp" /> 

     <TextView 
      android:id="@+id/other_player_points" 
      android:text="10" 
      android:gravity="right" 
      android:layout_width="30dp" /> 
    </TableRow> 

    </TableLayout> 


</LinearLayout> 
+0

Textview是否動態? – Libin

+0

您必須在for循環中創建textview .. – Riser

+0

textfields的文本對每一行都不相同 – Deca

回答

0

您在不同的行添加ownPlayerpointsOwnPlayerenemyPlayerpointsEnemyPlayer幾次。但一個觀點只能有一個家長。

相關問題