Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
@Override
public boolean onOptionsItemSelected(MenuItem item){
    //change the spinners item depending on menu choice
    switch(item.getItemId()){
    case R.id.lengthMenu:
        adapter = ArrayAdapter.createFromResource(this, R.array.lengthArray, android.R.layout.simple_spinner_item);
        adapter.notifyDataSetChanged();
        return true;
    case R.id.weightMenu:
        adapter = ArrayAdapter.createFromResource(this, R.array.weigthArray, android.R.layout.simple_spinner_item);
        adapter.notifyDataSetChanged();
        return true;
    case R.id.liquidsMenu:
        adapter = ArrayAdapter.createFromResource(this, R.array.liquidsArray, android.R.layout.simple_spinner_item);
        adapter.notifyDataSetChanged();
        return true;
    default:
        return true;
    }


The problem is that when i run this code it doesn't change the spinner values/content and it sticks with the adapter value given in onCreate(). Also 2 spinners are using this data and all data is in a string-array in strings.xml.

Here is onCreate if needed:

Java
ArrayAdapter<CharSequence> adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    answer = (TextView) findViewById(R.id.answer);
    unitSize = (EditText) findViewById(R.id.unitSize);

    adapter = ArrayAdapter.createFromResource(this, R.array.lengthArray, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    firstUnit = (Spinner) findViewById(R.id.firstUnit);
    secondUnit = (Spinner) findViewById(R.id.secondUnit);
    firstUnit.setOnItemSelectedListener(this);
    secondUnit.setOnItemSelectedListener(this);
    firstUnit.setAdapter(adapter);
    secondUnit.setAdapter(adapter);
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900