This is a snippet of the code in my Main Activity.
RadioGroup radioGroup;
TextView TxtView;
Spinner spinner;
TextView result;
ArrayList<String> countries;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countries = new ArrayList<>();
countries.add("Select");
countries.add("Spain");
countries.add("Italy");
countries.add("Rome");
countries.add("UK");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, countries);
adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
radioGroup = findViewById(R.id.radioGroup);
result = findViewById(R.id.result);
spinner = findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != 0)
result.setText("You have selected " + parent.getSelectedItemId());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButton)
Toast.makeText(getApplicationContext(), "You clicked Button 1", Toast.LENGTH_LONG).show();
else if (checkedId == R.id.radioButton2)
Toast.makeText(getApplicationContext(), "You clicked Button 2", Toast.LENGTH_LONG).show();
});
}
}
I want to find a way to link the spinner to different radio buttons. So that each radio button changes the set of options shown in the spinner. Can anyone help with the code?