I want to build a form with radio buttons and a submit button. I use a FieldChangeListener to capture selection. I think getSelectedIndex() is supposed to retrieve button selected, but I keep getting a null when the button is clicked. I was able to overcome this with an inner class on radiobuttongroup , but I know it is not the best solution.
Can anyone advise?
PHP Code:
public class fieldTest extends MainScreen implements FieldChangeListener
{
ButtonField button1 = null;
RadioButtonField radioButtonF1 = null;
RadioButtonField radioButtonF2 = null;
RadioButtonField radioButtonF3 = null;
int valueOfRadioButton = 0;
public fieldTest()
{
super(DEFAULT_MENU | DEFAULT_CLOSE);
LabelField title = new LabelField("RadioButtonField Example" ,LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
LabelField lblA, lblB;
lblA = new LabelField("Here comes some radioButtonFields in a RadioButtonGroup:");
this.add(lblA);
final RadioButtonGroup rbGroup = new RadioButtonGroup();
RadioButtonField radioButtonF1 = new RadioButtonField("Radio Option 1", rbGroup, true); // turn the first one "on"
RadioButtonField radioButtonF2 = new RadioButtonField("Radio Option 2", rbGroup, false);
RadioButtonField radioButtonF3 = new RadioButtonField("Radio Option 3", rbGroup, false);
this.add(radioButtonF1);
this.add(radioButtonF2);
this.add(radioButtonF3);
button1 = new ButtonField("Submit");
this.add(button1);
button1.setChangeListener(this);
rbGroup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context)
{ if(rbGroup.getSelectedIndex() == 0)
valueOfRadioButton = 0;
if(rbGroup.getSelectedIndex() == 1)
valueOfRadioButton = 1;
if(rbGroup.getSelectedIndex() == 2)
valueOfRadioButton = 2;
}
});
radioButtonF1.setChangeListener(this);
radioButtonF2.setChangeListener(this);
radioButtonF3.setChangeListener(this);
}
// over-ride default onSavePrompt method to avoid being asked if I want to save each time.
public boolean onSavePrompt()
{
return true;
}
public void fieldChanged(Field field, int context)
{
if(field instanceof ButtonField)
{
if(valueOfRadioButton == 0)
Dialog.alert("Option One was selected!");
if(valueOfRadioButton == 1)
Dialog.alert("Option Two was selected!");
if(valueOfRadioButton == 2)
Dialog.alert("Option Three was selected!");
}
}
}