Reputation: 49
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.
Upvotes: 1
Views: 3659
Reputation: 45503
You are probably getting an UnsupportedOperationException
, right? This is because the adapters get initialised with an array of objects, which it interally converts to an AbstractList
, which cannot be modified.
To solve your problem, you simply need to feed something that implements the List<?>
interface to the adapter. Example:
String[] strings = getResources().getStringArray(R.array.cars);
List<String> items = new ArrayList<String>(Arrays.asList(strings));
ArrayAdapter<String> adMan = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
Upvotes: 7
Reputation: 6517
Hoh, can't believe you have the same problem as I did some hours ago. I already reported a bug about this: bug report
The issue is that when you pass an array to the constructor it actually calls the other constructor overload that takes a List instead of Array with Arrays.asList(array)
. However, this method only returns a view on the backing array and hence will not allow adding and removing elements to the list. You can star the bug report in the android issue tracker if you feel like it.
You can also view the code of the ArrayAdapter class here for example
Upvotes: 0