tags:

views:

49

answers:

1

How can i retrieve the text of a dynamically created radio button selected by the user? Here's my code:

RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1); 
        // layout params to use when adding each radio button 
        LinearLayout.LayoutParams layoutParams = new 
RadioGroup.LayoutParams( 
                RadioGroup.LayoutParams.WRAP_CONTENT, 
                RadioGroup.LayoutParams.WRAP_CONTENT); 
 for (int i = 0; i < 4; i++){ 
            final RadioButton newRadioButton = new RadioButton(this); 
            c3 = db.getAns(3); 
        for (int j=0;j<i;j++) 
            c3.moveToNext(); 
           label = c3.getString(0); 
        newRadioButton.setText(label); 
        newRadioButton.setId(6); 
        radiogroup.addView(newRadioButton, layoutParams); 

Waiting for the reply, Maqsood

+2  A: 

Surprised there isn't an easier way. If you are going to do something special though based on which button you should probably checked the ID instead of the Label.

radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
         void onCheckedChanged(RadioGroup rg, int checkedId) {
              for(int i=0; i<rg.getChildCount(); i++) {
                   RadioButton btn = (RadioButton) rg.getChildAt(i);
                   if(btn.getId().equals(checkedId) {
                        String text = btn.getText();
                        // do something with text
                        return;
                   }
              }
         }
    });
BrennaSoft
Thanks but there is no such method as equals(checkedId) of btn.getId(). I tried that in my Eclipse editor but can't find it.Please check.
Maxood
then just do getId() == checkedId. The id is just an int.
BrennaSoft