Reputation: 117
So I have an app where I have several drop down spinners several of which I would like to change their options based on the option selected by another spinner. My plan to do this was to put:
product_adapter.clear();
CharSequence[] array=makeArray(urlMaker.getProductid());
for(int i=0;i<array.length;i++){
product_adapter.add(array[i]);
}
product_adapter.notifyDataSetChanged();
within a case of the onItemSelected() method for the spinner that is to dictate the changing of contents of the other spinner. In theory the idea is that I clear the second spinner(product_adapter) then add a new array that is made by makeArray()(which returns an array of CharSequences as specified in the doc) to the spinner's adapter using product_adapter.add(array[i]) then call .notifyDataSetChanged() to formalize it. However, when it hits the first line the app crashes and yields the following stack trace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.benhouse.weatherview, PID: 25791
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:638)
at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
at java.util.AbstractList.removeRange(AbstractList.java:658)
at java.util.AbstractList.clear(AbstractList.java:466)
at android.widget.ArrayAdapter.clear(ArrayAdapter.java:273)
at com.example.benhouse.weatherview.MainActivity$3.onItemSelected(MainActivity.java:102)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:924)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:913)
at android.widget.AdapterView.-wrap1(AdapterView.java)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:883)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Why am I getting this error and how can I fix it?
Edit: this is how I am intially populating the spinner that I am trying to change:
ArrayAdapter<CharSequence> T_spinnerAdapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, makeArray("GFS"));
Time_spin.setAdapter(T_spinnerAdapter);
Edit 2: So I am a bit of an idiot. I was trying to modify product spinner rather than time spinner(the intended spinner) and the product spinner is initialized from an array in the values.xml. Also probably doesn't like modifying itself within itself. Thanks for the indirect help as it lead me to think and undummy-ify my code.
Upvotes: 0
Views: 1758
Reputation: 7793
You are using list collection which doesn't support remove operation.
If you are constructing ArrayAdapter
by providing the array of items:
ArrayAdapter (Context context,
int resource,
T[] objects)
Actual implementation creates fixed length list using Arrays.asList()
Fix:
ArrayList list = new ArrayList(Arrays.asList(makeArray("GFS")));
new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, list);
It is a common mistake. Another common mistake is providing an immutable list.
Upvotes: 4