I get an error on the “button2.setOnClickListener(radio_listener);”
Because its embedded inside the onclick listener calling itself, whats the way around this?
final RadioGroup rGroup3 = (RadioGroup) findViewById(R.id.radio_gcam);
OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
RadioButton button = (RadioButton) v;
no_of_gcams = 3;
gcam_names_array[0] = "Machine 1";
gcam_names_array[1] = "Machine 2";
gcam_names_array[2] = "Machine 3";
gcam_names_array[3] = "Machine 4";
for (int i = 0; i < no_of_gcams; i++)
{
RadioButton button2 = new RadioButton(ModbusDroid.this);
button2.setText(gcam_names_array[i]);
button2.setOnClickListener(radio_listener);
rGroup3.addView(button2);
}
}
};
if (no_of_gcams == 0)
{
RadioButton button1 = new RadioButton(this);
button1.setText("Setup a G-CAM");
button1.setOnClickListener(radio_listener);
rGroup3.addView(button1);}
Try the below snippet and it will help: