2012-01-04 5 views
1

2つのテーブルを画面に表示しています。 最初のテーブルの表示に問題はありません。 TableLayout second_table、のTableRow tableRow5を参照 は、テキストフィールド(S)textView51、textView52、textView53java.lang.IllegalStateException:指定された子にはすでに親があります。最初に子の親に対してremoveView()を呼び出す必要があります。

テキストフィールドは、基本的にカラムです。表示する3つの列 名、姓、年齢。サーバーから取得した値を動的に設定する必要があります。

私は以下の例外を抱えています: java.lang.IllegalStateException:指定された子には既に親があります。子の親で最初にremoveView()を呼び出す必要があります。

私は、次のコード行を使用して第2のテーブルを移入しています:

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.employee_details); 
TableLayout tableLay = (TableLayout) findViewById(R.id.second_table); 
TableRow tableRow = (TableRow) findViewById(R.id.tableRow5); 
TextView fNameTxtView = (TextView) findViewById(R.id.textView51); 
TextView lNameTxtView = (TextView) findViewById(R.id.textView52); 
TextView ageTxtView = (TextView) findViewById(R.id.textView53); 
for(int count = 0; count < tempArrList.size() ; count++){ 
fNameTxtView.setText(tempArrList.get(temp.get(0))); 
lNameTxtView.setText(tempArrList.get(temp.get(1))); 
ageTxtView.setText(tempArrList.get(temp.get(2))); 
    tableRow.addView(fNameTxtView); 
    tableRow.addView(lNameTxtView); 
    tableRow.addView(ageTxtView); 
    tableLay.addView(tableRow); // Add the new row to our tableLayout 
} 
} 

私もいくつかのバリエーションを試してみましたが、問題を解決することができませんでしてきました。 提案が歓迎されます。

レイアウトXMLは次のとおりです。あなたが挿入

tableRow.addView(fNameTxtView); 
    tableRow.addView(lNameTxtView); 
    tableRow.addView(ageTxtView); 
    tableLay.addView(tableRow); 

employee_details.xmlあなたが同じTextViewTableRowオブジェクトとあなたがすべての時間を使用しているあなたのfor声明で

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <include android:id="@+id/header" layout="@layout/activity_header" /> 
    <RelativeLayout android:id="@+id/RelativeLayout1" 
     android:background="@drawable/footerbg" android:layout_height="wrap_content" 
     android:layout_width="fill_parent" android:layout_alignParentBottom="true" 
     android:gravity="center"> 
     <Button android:layout_alignParentTop="true" 
      android:layout_height="50dp" android:layout_width="150dp" android:id="@+id/btn_done"></Button> 
    </RelativeLayout> 
    <ScrollView android:id="@+id/scrollView01" 
     android:layout_above="@id/RelativeLayout1" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_below="@+id/header"> 
     <LinearLayout android:id="@+id/linear" 
      android:orientation="vertical" android:layout_gravity="center_horizontal" 
      android:layout_width="match_parent" android:layout_height="match_parent"> 
      <TextView android:id="@+id/hello_TxtView" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      </TextView> 
      <TableLayout android:id="@+id/first_table" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" 
       android:background="@drawable/table_shape"> 
       <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 
        <TextView android:id="@+id/textView11" 
         android:layout_width="fill_parent" android:layout_height="match_parent"> 
        </TextView> 
        <TextView android:id="@+id/textView12" 
         android:layout_width="fill_parent" android:layout_height="wrap_content"> 
        </TextView> 
       </TableRow> 
      </TableLayout> 
      <TableLayout android:id="@+id/second_table" 
       android:layout_width="match_parent" android:layout_height="match_parent"> 
       <TableRow android:id="@+id/tableRow4" android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 
        <TextView android:id="@+id/textView41" 
         android:layout_width="match_parent" android:layout_height="wrap_content"> 
        </TextView> 
        <TextView android:id="@+id/textView42" 
         android:layout_width="fill_parent" android:layout_height="wrap_content"> 
        </TextView> 
        <TextView android:id="@+id/textView43" 
         android:layout_width="fill_parent" android:layout_height="wrap_content"> 
        </TextView> 
       </TableRow> 
       <TableRow android:id="@+id/tableRow5" android:layout_width="match_parent" 
        android:layout_height="wrap_content" android:background="#f6f7fc"> 
        <TextView android:id="@+id/textView51" 
         android:layout_width="match_parent" android:layout_height="wrap_content"> 
        </TextView> 
        <TextView android:id="@+id/textView52" 
         android:layout_width="match_parent" android:layout_height="wrap_content"> 
        </TextView> 
        <TextView android:id="@+id/textView53" 
         android:layout_width="match_parent" android:layout_height="wrap_content"> 
        </TextView> 
       </TableRow> 
      </TableLayout> 
     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

答えて

3

あなたのレイアウトで同じTextViewのfNameTxtView、lNameTxtView、ageTxtView。

for(int count = 0; count < tempArrList.size() ; count++){ 
TextView fNameTxtView = new TextView(this); 
fNameTxtView.setText(tempArrList.get(temp.get(0))); 
TextView lNameTxtView= new TextView(this); 
lNameTxtView.setText(tempArrList.get(temp.get(1))); 
TextView ageTxtView = new TextView(this); 
ageTxtView.setText(tempArrList.get(temp.get(2))); 
TableRow tableRow = new TableRow(this); 
    tableRow.addView(fNameTxtView); 
    tableRow.addView(lNameTxtView); 
    tableRow.addView(ageTxtView); 
    tableLay.addView(tableRow); // Add the new row to our tableLayout 
} 

編集:あなたがそれらの多くの作業を行いたい場合はもちろん、あなたがTextViewsのIDを追加することができ、あなたのこのようなステートメントのために動的にオブジェクトを作成することができ、これを回避するために 。

関連する問題