2011-11-04 2 views
0

mon fichier XML de Radiobuttonajouter dynamiquement

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


<RadioGroup 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/rgc"> 
</RadioGroup> 


</LinearLayout> 

fichier Java

LinearLayout l1; 
RadioGroup rg; 
RadioButton rb[]; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    l1=(LinearLayout)findViewById(R.id.sl); 
    rg=(RadioGroup)findViewById(R.id.rgc); 


    rb=new RadioButton[4]; 
    for(int i=0;i<4;i++){ 
     rb[i]=new RadioButton(this); 
     rb[i].setLayoutParams(new LinearLayout.LayoutParams(60,30)); 
     rb[i].setText(i+"aaaaaa"); 
     rg.addView(rb[i]); 

    } 

    l1.addView(rg); 



} 

quand je suis en cours d'exécution de ce code se exception "cet enfant spécifié a déjà un parent" Veuillez me dire à ses amis quelle est la problème dans mon code et de bien vouloir me donner des suggestions.

Répondre

2

juste ne pas ajouter rg à disposition linéaire Comme il est radiogroup déjà présent dans le LinearLayout dans le fichier xml

+0

@ekjyot ... merci beaucoup ..... – sarath

+0

U r WElcome @sarath – ekjyot

0

Essayez ce code, espère que cela aidera

RadioGroup rg=(RadioGroup)findViewById(R.id.rgc); 
     RadioButton rb; 

     for(int i=0;i<4;i++) 
     { 
      rb=new RadioButton(this); 
      rb.setText(i+"aaaaaa"); 
      rg.addView(rb,i,new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

     } 

     rg.invalidate(); 
+0

... bonne suggestion ..... – sarath