I am using an ArrayAdapter
to populate the items to list in a android.widget.Spinner
. That works all fine.
But now I wa
In your adapter Class - Delete an Item
remove(position);
notifyDataSetChanged();
Add an Item -
adapter.add (newItem);
adapter.notifyDataSetChanged ();
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>
).
I know it's late but just a quick explanation: it's because method Arrays.asList(T... array) returns custom inner class named ArrayList that is read-only. As already said, you need to provide full impl. e.g. java.util.ArrayList.
You can try like this:
new ArrayList<>(Arrays.asList(recentlyClient))
Example code how to implement:
String[] recentlyClient;
ArrayAdapter<String> recenAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,new ArrayList<>(Arrays.asList(recentlyClient)));
Probably, you are using List in your ArrayAdapter class instead of ArrayList.
Try converting your array or list to ArrayList -
new ArrayList<ClassType>(Arrays.asList(array));
I was having the same problem, my data was saved in resource String Array, so I was creating ArraAdapter with createFromResource.
The following code for creating ArrayAdapter from resource String Array solved the problem:
Resources res = getResources();
String[] cities = res.getStringArray(R.array.cities_array);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
new ArrayList(Arrays.asList(cities)));