I am using an ArrayAdapter<CharSequence>
to populate the items to list in a android.widget.Spinner
. That works all fine.
But now I want to keep the list of items dynamic, i.e. I want to be able to add/remove items from the selection list at runtime. However, when I call adapter.add(item)
or adapter.remove(item)
I always get a UnsupportedOperationException
, even though the Javadocs of the ArrayAdapter
class describe these two methods as to be usable for exactly that intended purpose.
Is this a bug, really not implemented or what am I missing here?
Create a global ArrayList and add the contents to it using add() and pass it to ArrayAdapter. It's better to pass the List or String[] to ArrayAdapter and set that adapter to List You should update the List or Array being passed to Adapter, not Adapter itself.
Just call submitList with a new list, the diffutil will take care of the old list and the new list. If you add or remove something, add to your original list or remove an item from your list and then call submitList. you can call use adapter.
Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.
You probably initialized the adapter with a plain Java array (e.g., String[]
). Try using something that implements the java.util.List
interface (e.g., ArrayList<String>
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With