I am a newbie to Android programming.
I want two radio buttons that when clicked it will change the spinner array.
I know it sounds simple. I have it all working but I am not sure how to implement the onclickchange to make this happen. I have attached the code.
I am having an awful time trying to get my head around how to set this listener. Also, I have no errors, and spinner works, just can’t get it to change arrays.
Any guidance is greatly appreciated.
Cheers,
I attempted to create it with the listener, but it doesnt work, any advice is appreciated.
Xml
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/radiobutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/radio1" />
<RadioButton
android:id="@+id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio2" />
</RadioGroup>
java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.canprovselect);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup1);
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = null;
String radioButtonSelected = "";
switch (checkedRadioButton) {
case R.id.radiobutton3 : radioButtonSelected = "radiobutton1";
adapter = ArrayAdapter.createFromResource(
this, R.array.prov_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelected(
));
break;
case R.id.radiobutton4 : radioButtonSelected = "radiobutton2";
adapter = ArrayAdapter.createFromResource(
this, R.array.prov1_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelected(
));
break;
}
My attempt at a listener
public class CanProvselect extends Activity {
RadioButton myOption1, myOption2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.canprovselect);
myOption1 = (RadioButton)findViewById(R.id.radio_1);
myOption2 = (RadioButton)findViewById(R.id.radio_2);
myOption1.setOnClickListener(myOptionOnClickListener);
myOption2.setOnClickListener(myOptionOnClickListener);
OnClickListener myOptionOnClickListener = null;
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = null;
}
RadioButton.OnClickListener myOptionOnClickListener =
new RadioButton.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//switch (checkedRadioButton) {
myOption1 : radioButtonSelected = "radio_1";
adapter = ArrayAdapter.createFromResource(
this, R.array.prov_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelected(
));
myOption2 : radioButtonSelected = "radio_2";
adapter = ArrayAdapter.createFromResource(
this, R.array.prov1_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelected(
));
}
}
}
You need to set radioGroup.setOnClickListener(yourListenerClass). Here is an example.Radio button onclick event
In that there is onClick method with Toast message, replace that with your listview population code.