2013-10-11 90 views
0

我有一个布局,每次按下我的按钮我想添加一组存储在另一个布局中的视图。这是我到目前为止的尝试方式。 logcat中的错误是:如何用布局充填器填充相同的视图?

“E/AndroidRuntime(7900):java.lang.IllegalStateException:指定的子项已经有父项,必须先调用子项的父项的removeView()。

代码:

material_cost.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/materialCostText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="Κόστος Υλικών" 
      android:textColor="#FF0000" 
      android:textSize="16sp" 
      android:textStyle="bold" 
      android:gravity="center_horizontal" 
      android:layout_margin="5dp"/> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="2dp" 
      android:layout_marginBottom="8dp" 
      android:layout_marginTop="4dp" 
      android:background="#33B5E5" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/materialWrapper"> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp"> 

     <Button 
      android:id="@+id/btnMaterialSubmit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Αποθήκευση" 
      android:layout_weight="50"/> 
     <Button 
      android:id="@+id/btnMaterialAdd" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Προσθήκη πεδίου" 
      android:layout_weight="50"/> 
    </LinearLayout> 
</LinearLayout> 
</ScrollView> 

material_cost_item.xml

<?xml version="1.0" encoding="utf-8"?> 


<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/materialItem" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/materialText" 
      android:text="Υλικό" 
      android:textSize="16sp" 
      android:textStyle="italic" 
      android:layout_weight="50" 
      android:layout_marginLeft="5dp"/> 
     <EditText 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_weight="50" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/materialText" 
      android:text="Τιμή μονάδας" 
      android:textSize="16sp" 
      android:textStyle="italic" 
      android:layout_weight="50" 
      android:layout_marginLeft="5dp"/> 
     <EditText 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_weight="50" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/materialText" 
      android:text="Ποσότητα" 
      android:textSize="16sp" 
      android:textStyle="italic" 
      android:layout_weight="50" 
      android:layout_marginLeft="5dp"/> 
     <EditText 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_weight="50" 
      /> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
     <View 
      android:layout_width="match_parent" 
      android:layout_height="2dp" 
      android:layout_marginBottom="8dp" 
      android:layout_marginTop="4dp" 
      android:background="#33B5D8" /> 
    </LinearLayout> 

    </LinearLayout> 

最后我的课

public class MaterialActivity extends Activity implements OnClickListener { 

    Button btnAdd; 
    LinearLayout rootLayout; 
    View layoutItem; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.material_cost); 

     btnAdd = (Button) findViewById(R.id.btnMaterialAdd); 


     rootLayout = (LinearLayout) findViewById(R.id.materialWrapper); 
     layoutItem = getLayoutInflater().inflate(R.layout.material_cost_item, rootLayout,false); 
     rootLayout.addView(layoutItem); 

     btnAdd.setOnClickListener(this); 
    } 

    public void onClick(View v){ 

     switch (v.getId()){ 
     case R.id.btnMaterialAdd:{ 
      inflateEntry(); 
      break; 
     } 
     } 
    } 

    private void inflateEntry() { 
     // TODO Auto-generated method stub 
     Log.v("debug","pressed"); 
     rootLayout.addView(layoutItem); 
    } 
} 

回答

1

你需要每次再膨胀的观点您按一下按钮:

private void inflateEntry() 
{ 
    // TODO Auto-generated method stub 
    Log.v("debug","pressed"); 
    View layoutItem = getLayoutInflater().inflate(R.layout.material_cost_item, null); 
    rootLayout.addView(layoutItem); 
} 
+0

你是我的救命恩人谢谢了很多! – sarakinos

+1

@sarakinos:然后将其标记为已接受的答案。这里是[你怎么做到的。](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – gunar