Unable to modify ArrayAdapter in ListView: UnsupportedOperationException
I tried it out, myself...Found it didn't work. So i check out the source code of ArrayAdapter and found out the problem. The ArrayAdapter, on being initialized by an array, converts the array into a AbstractList (List) which cannot be modified.
Solution
Use an ArrayList<String>
instead using an array while initializing the ArrayAdapter.
String[] array = {"a","b","c","d","e","f","g"};
ArrayList<String> lst = new ArrayList<String>(Arrays.asList(array));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lst);
Cheers!
Custom ListView Adapter throws UnsupportedOperationException
Looking around a bit, it would seem that initializing the adapter with an array is the problem. See UnsupportedOperationException with ArrayAdapter.remove and Unable to modify ArrayAdapter in ListView: UnsupportedOperationException
Try using an ArrayList
instead of an array
like so
ArrayList<Weather> weather_data = new ArrayList<Weather>()
weather_data.add( new Weather(R.drawable.weather_cloudy, "Cloudy") );
// continue for the rest of your Weather items.
If you're feeling lazy, you can convert your array
to an ArrayList
this way
ArrayList<Weather> weatherList = new ArrayList<Weather>();
weatherList.addAll(Arrays.asList(weather_data));
To finish the conversion to ArrayList
in your WeatherAdapter
class you will want to remove the Weather data[] = null;
and all of it's references (such as inside the constructor) because ArrayAdapter
holds the data for you and you can access it with getItem
So inside of your getView
function you would change Weather weather = data[position];
to Weather weather = getItem(position);
Update
Modify your udated code with
private void setListViewAdapterToDate(int month, int year, int dv)
{
setListView(month, year, dv);
if(summaryAdapter != null) {
summaryAdapter.clear();
summaryAdapter.addAll( summaryList );
summaryAdapter.notifyDataSetChanged();
} else {
summaryList.addAll(Arrays.asList(summary_data));
summaryAdapter = new SummaryAdapter(this.getActivity().getApplicationContext(), R.layout.listview_item_row, summaryList);
}
calendarSummary.setAdapter(summaryAdapter);
}
UnsupportedOperationException while adding a string to a list through ArrayAdapter
Yeah as suspected, your ArrayAdapter
is backed by an array
.
Array
does not support the add operation.
Change it to be back by an ArrayList. ie:
private final static List<String> items = new ArrayList();
static {
items.add("this");
items.add("is");
items.add("a");
items.add("really");
items.add("silly");
items.add("list");
}
As WandMaker hinted, you seem to have done some work to create an ArrayList already, another solution could be:
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, words);
The optimum solution (with your code) being:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
List<String> words = new ArrayList<String>();
words.add("this");
words.add("is");
words.add("a");
words.add("really");
words.add("silly");
words.add("list");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, words);
setListAdapter(adapter);
}
UnsupportedOperationException with ArrayAdapter.remove
It seems that this problem crops up when you initialize your ArrayAdapter
with an array. Try initializing it with a List<JournalEntry>
. Reference: Why can't one add/remove items from an ArrayAdapter?
Refreshing Listview's ArrayAdapter with the remove method throws UnsupportedOperationException
If the adapter reference points to a default ArrayAdapter
instance then you most likely instantiate the ArrayAdapter
using an array
of objects as its source of data. If this is the case then, under the hood, the ArrayAdapter
will transform that array into a special ArrayList
(not the normal java one). This special ArrayList
doesn't implement the methods that change its size(so using methods like add
or remove
(which modify that list) on the ArrayAdapter
will throw the UnsupportedOperationException
), it will only allow you to modify the values in it.
If you want to use that remove
method then put the data from the array
that you currently use in the ArrayAdapter
in an ArrayList
and then pass that list to the ArrayAdapter
constructor.
Unable to modify values in ListView
Look this answer:
ArrayAdapter
So basically as in this answer said: "The ArrayAdapter, on being initialized by an array, converts the array into a AbstractList (List) which cannot be modified."
So it easy, create your own ArrayList, in your case of type String, and then assign this to CustomAdapter, then you can just remove items from your ArrayList and notifyDataSetChanged() should works.
Regards
Change values of ArrayAdapter
Pass your own list instead of array to adapter. Adapter converts array to list using Arrays.asList
method and list returned by this method doesn't support removal. You can create instance of ArrayList
from your array and pass it to adapter:
List<String> values = new ArrayList<String>(Arrays.asList(categoriesArray));
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
But better check very similar question mentioned by @PrafulBhatnagar: Unable to modify ArrayAdapter in ListView: UnsupportedOperationException
Related Topics
Singleton Object Becomes Null After App Is Resumed
Solution to Install_Failed_Insufficient_Storage Error on Android
Set Transparent Background of an Imageview on Android
Android: Upgrading Db Version and Adding New Table
Difference Between Oncreate() and Onstart()
Receive Result from Dialogfragment
How to View the SQLite Database on an Android Device
How to Encrypt and Decrypt File in Android
Android: How to Display Video Thumbnails
How to Detect Swipe Direction Between Left/Right and Up/Down
Proper Implementation of Viewpager2 in Android
How to Draw Interactive Polyline on Route Google Maps V2 Android
Android: Create Spinner Programmatically from Array
How to Make Bitmap Compress Without Change the Bitmap Size
How to Use Custom Font in a Project Written in Android Studio
Adding Local .Aar Files to Gradle Build Using "Flatdirs" Is Not Working