Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating an on Radio button on click event to change spinner array in java
    primarykey
    data
    text
    <p>I am a newbie to Android programming.</p> <p>I want two radio buttons that when clicked it will change the spinner array. </p> <p>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. </p> <p>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.</p> <p>Any guidance is greatly appreciated. Cheers,</p> <p>I attempted to create it with the listener, but it doesnt work, any advice is appreciated.</p> <p>Xml</p> <pre><code>&lt;RadioGroup android:id="@+id/radiogroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" &gt; &lt;RadioButton android:id="@+id/radiobutton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/radio1" /&gt; &lt;RadioButton android:id="@+id/radiobutton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio2" /&gt; &lt;/RadioGroup&gt; </code></pre> <p>java</p> <pre><code> @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&lt;CharSequence&gt; 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; } </code></pre> <p>My attempt at a listener</p> <pre><code>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&lt;CharSequence&gt; 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( )); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload