首先在孩子的父母上调用removeView

时间:2014-04-19 12:34:00

标签: android

我需要动态地将tablerows添加到我的表中。每行必须有4个TextView .. 当我运行程序时出现错误"你必须首先在孩子的父母身上调用removeView"。我该怎么办? 这是我的代码和我的xml:

@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>

1 个答案:

答案 0 :(得分:0)

您可以在不同的行中多次添加ownPlayerpointsOwnPlayerenemyPlayerpointsEnemyPlayer。但是视图只能有一个父级。