2016-08-01 4 views
1

테이블을 만들려고합니다. 그러나 행 수는 앱이로드 될 때마다 달라 지므로 프로그래밍 방식으로 추가합니다. 행에는 mainLayout을 제외하고 자체 XML 레이아웃이 있습니다. 그러나 mainlayout에서 tableview에 행을 추가하려고하면 오류가 발생합니다 (tableLayou.addView (row)). 여기에 내 코드 :모름으로 인해 뷰를 추가 할 수 없습니까?

private void makeTable() { 
    View v = getLayoutInflater().inflate(R.layout.row, null); 
    TableRow row; 
    AutofitTextView textView; 
    Configuration configuration = getResources().getConfiguration(); 
    int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
      (float) 1, getResources().getDisplayMetrics()); 
    int width = (configuration.screenWidthDp/8)*dip; 
    int height = (configuration.screenHeightDp/10)*dip; 
    v.setBackgroundColor(getResources().getColor(R.color.black)); 
    for (int i = 0; i<rowss.size();i++) { 

     row =(TableRow) v.findViewById(R.id.tableRow); 
     Scanner scanner = new Scanner(rowss.get(i)); 
     while(scanner.hasNextLine()) { 
      Scanner s = new Scanner(scanner.nextLine()); 
      AutofitTextView textView1 = (AutofitTextView) row.findViewById(R.id.manufacturer); 
      textView1.setText(s.next()); 
     } 

     tableLayout.addView(row); 

    } 

} 

Row.xml :

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/tableRow" 
    android:weightSum="8"> 
    <me.grantland.widget.AutofitTextView 
     android:id="@+id/manufacturer" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 

    <me.grantland.widget.AutofitTextView 
     android:layout_width="0dp" 
     android:id="@+id/name" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 

    <me.grantland.widget.AutofitTextView 
     android:layout_width="0dp" 
     android:id="@+id/reason" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 


    <me.grantland.widget.AutofitTextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:id="@+id/block" 
     android:layout_height="wrap_content" /> 


    <me.grantland.widget.AutofitTextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:id="@+id/carrier" 
     android:layout_height="wrap_content" /> 


    <me.grantland.widget.AutofitTextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:id="@+id/quantity" 
     android:layout_height="wrap_content" /> 


    <me.grantland.widget.AutofitTextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:id="@+id/inventory" 
     android:layout_height="wrap_content" /> 

    <CheckBox 
     android:layout_width="0dp" 
     android:id="@+id/status" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 




</TableRow> 

activity_main :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/content_main"> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button"/> 

    <TableLayout 
     android:id="@+id/table" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal"/> 
</LinearLayout> 

오류 : 모든

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
                         at android.view.ViewGroup.addViewInner(ViewGroup.java:4654) 
                         at android.view.ViewGroup.addView(ViewGroup.java:4490) 
                         at android.widget.TableLayout.addView(TableLayout.java:426) 
                         at android.view.ViewGroup.addView(ViewGroup.java:4431) 
                         at android.widget.TableLayout.addView(TableLayout.java:408) 
                         at android.view.ViewGroup.addView(ViewGroup.java:4404) 
                         at android.widget.TableLayout.addView(TableLayout.java:399) 
                         at com.intellidev.earlyupgradepicklist.MainActivity.makeTable(MainActivity.java:460) 
                         at com.intellidev.earlyupgradepicklist.MainActivity.access$300(MainActivity.java:64) 
                         at com.intellidev.earlyupgradepicklist.MainActivity$MakeRequestTask.onPostExecute(MainActivity.java:411) 
                         at com.intellidev.earlyupgradepicklist.MainActivity$MakeRequestTask.onPostExecute(MainActivity.java:331) 
                         at android.os.AsyncTask.finish(AsyncTask.java:651) 
                         at android.os.AsyncTask.access$500(AsyncTask.java:180) 
                         at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                         at android.os.Looper.loop(Looper.java:158) 
                         at android.app.ActivityThread.main(ActivityThread.java:7229) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

답변

0

첫째. 당신의 목표는 무엇입니까? TableLayout을 List로 사용 하시겠습니까? 그렇다면 ListView를 사용해야 할 수도 있고 사용해야 할 수도 있습니다.

The point of using ListView is to be able to scale to larger data sets by not having to create and layout views for all of the items up-front.

순간에 문제는 당신이주기를 실행 때마다, 당신은 TableLayout을

row =(TableRow) v.findViewById(R.id.tableRow); 
...   
tableLayout.addView(row); 

에 동일한 개체를 계속 추가 할 것입니다 그리고 그것은 이미 존재하기 때문에, 그것은 '지정된 발생 자식에게는 이미 부모가 있습니다. 당신은
은 무엇 당신이 할 수있는 것은 사용자가 만든 모든 TableRow 뷰를 포함 ArrayAdapter와이입니다 첫 아이의 부모에 removeView()를 호출하고, 다음과 같이 추가해야합니다 :

int count = adapter.getCount(); 
for (int i = 0; i < count; i++) { 
    tableLayout.addView(createTableRow(adapter.getItem(i)); // or tableLayout.addView(adapter.getView(i, null, tableLayout)); 
} 

전반적으로, 당신의 table_row. XML은 당신이 당신은

You can find documentation for the ListView here 및 (행에서 중첩 된 데이터를 처리 할 수있는 적절한 요소 인) ListView를위한 TableLayout을 전환 할 수 등의 제조 업체, ID를,이리스트에 엔트리 것 같아 Check this example for a ListView with CustomAdapter

어느 쪽이든, 내가 도와 드리겠습니다. 감사합니다,

관련 문제