I’m trying to get a couple spinners to change dynamically based on their previous spinner selections. I can update the list but when i use adapter.clear() it crashes. Here is my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class CarKitsAct extends Activity{
ArrayAdapter<String> adMod, adEd;
String[] models, edition;
Boolean initSpMan = true;
Boolean initSpMod = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.carkitslayout);
// Construct TextViews
TextView textMan = (TextView) findViewById(R.id.textMan);
textMan.setText(R.string.selectmanufacturer);
TextView textMod = (TextView) findViewById(R.id.textMod);
textMod.setText(R.string.selectmodel);
TextView textEd = (TextView) findViewById(R.id.textEd);
textEd.setText(R.string.selectedition);
// Construct Spinners
Spinner spMan = (Spinner) findViewById(R.id.spMan);
Spinner spMod = (Spinner) findViewById(R.id.spMod);
Spinner spEd = (Spinner) findViewById(R.id.spEd);
// Construct Manufacturer Spinner Adapter
ArrayAdapter<CharSequence> adMan;
adMan = ArrayAdapter.createFromResource(this, R.array.cars, android.R.layout.simple_spinner_item);
adMan.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spMan.setAdapter(adMan);
// Set initial values for model and edition spinners
models = getResources().getStringArray(R.array.AC);
edition = getResources().getStringArray(R.array.ACAcceca);
//Construct adapters for models and editions
adMod = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, models);
adMod.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adMod.setNotifyOnChange(true);
spMod.setAdapter(adMod);
adEd = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, edition);
adEd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adEd.setNotifyOnChange(true);
spEd.setAdapter(adEd);
// Set up listeners for item selection
spMan.setOnItemSelectedListener(new ManItemSelectedListener());
}
public class ManItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (initSpMan == true) {
initSpMan = false;
} else {
models = getResources().getStringArray(2130968577 + pos);
adMod.clear();
adMod.addAll(models);
adMod.notifyDataSetChanged();
}
}
public void onNothingSelected(AdapterView<?> parent) {}
}
}
As you can see I tried using a boolean flag to determine whether the spinners have just been created or not but then when I change a selection it dies.
You are probably getting an
UnsupportedOperationException, right? This is because the adapters get initialised with an array of objects, which it interally converts to anAbstractList, which cannot be modified.To solve your problem, you simply need to feed something that implements the
List<?>interface to the adapter. Example: