2014-11-09 93 views
2

無法在表格佈局中實際添加表格行。我在這裏創建一個視圖滾動的水平和垂直方向都與視圖類似表有問題將TableRow表格行指定的孩子已經有父母。您必須先調用子視圖的父級上的removeView()

/* 
* reportLayout is tablelayout by findViewById 
* i am setting view inside Fragment 
*/ 
public void redrawReport(){ 
    reportLayout.removeAllViews(); 
    int count=0; 
    for(SaleReportModel model:Detailist.getReportItems()){ 
     TableRow headerRow= new TableRow(getActivity()); 
     headerRow.removeAllViews(); 

     //Error Caused here in this line 

     headerRow.addView(getTextView(model.getPrdcode(), false,count*4),count); 
     headerRow.addView(getTextView(model.getSubtype(), false,count*4+1),count); 
     headerRow.addView(getTextView(""+model.getDispatchQty(), false,count*4+2),count); 
     headerRow.addView(getTextView(""+model.getDispatchvalue(), false,count*4+3),count); 
     reportLayout.addView(headerRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT)); 
     count+=1; 
    } 
} 
public TextView getTextView(String data,boolean isHeader,int id){ 
    TextView textView= new TextView(getActivity()); 
    textView.setLayoutParams(new LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT)); 
    textView.setText(data); 
    textView.setId(id); 
    textView.setGravity(Gravity.CENTER); 
    //textView.setTextColor(Color.BLACK); 
    return mDateFrom; 

} 

錯誤日誌:

11-09 12:04:40.299: E/AndroidRuntime(19569): FATAL EXCEPTION: main 
11-09 12:04:40.299: E/AndroidRuntime(19569): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
11-09 12:04:40.299: E/AndroidRuntime(19569): at android.view.ViewGroup.addViewInner(ViewGroup.java:3739) 
11-09 12:04:40.299: E/AndroidRuntime(19569): at android.view.ViewGroup.addView(ViewGroup.java:3610) 
11-09 12:04:40.299: E/AndroidRuntime(19569): at android.view.ViewGroup.addView(ViewGroup.java:3555) 
11-09 12:04:40.299: E/AndroidRuntime(19569): at com.kiss.erp.view.fragment.SalesFragment.redrawReport(SalesFragment.java:254) 
11-09 12:04:40.299: E/AndroidRuntime(19569): at com.kiss.erp.view.fragment.SalesFragment.showdetail(SalesFragment.java:196) 
11-09 12:04:40.299: E/AndroidRuntime(19569): at com.kiss.erp.view.fragment.SalesFragment.onClick(SalesFragment.java:110) 

請幫助。

可能是由TableLayout的父項造成的,所以在這裏查看xml。

<ScrollView 

android:id="@+id/layoutReport" 
android:layout_height="match_parent"   
android:scrollbars="horizontal|vertical" 
android:layout_width="match_parent"  
android:layout_marginTop="5dip"  
android:scrollbarStyle="outsideInset" 
android:fillViewport="true"> 

<HorizontalScrollView 
    android:id="@+id/horizontalView" 
    android:layout_height="wrap_content"  
    android:scrollbars="horizontal|vertical" 
    android:layout_width="wrap_content"  
    android:layout_marginTop="5dip"> 

    <TableLayout 
     android:id="@+id/tlGridTable" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
</TableLayout> 
</HorizontalScrollView> 

在此先感謝。

回答

4

在方法getTextView中,您將返回mDateForm而不是textView。 由於mDateForm已經有父視圖,因此您會收到一個異常。 正確實施應該是這樣的:

public TextView getTextView(String data,boolean isHeader,int id){ 
    TextView textView= new TextView(getActivity()); 
    textView.setLayoutParams(new LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT)); 
    textView.setText(data); 
    textView.setId(id); 
    textView.setGravity(Gravity.CENTER); 
    //textView.setTextColor(Color.BLACK); 
    return textView; 

} 
+0

日Thnx, 我忘了愚蠢的事情。 – 2014-11-09 08:56:00

相關問題