4

This is the tutorial that I followed to use a custom Listview Adapter. The problem I am having is that when I try to clear the adapter, the app crashes and throws java.lang.UnsupportedOperationException

if(adapter != null) {
    adapter.clear();
}

UPDATED CODE:

private void setListViewAdapterToDate(int month, int year, int dv)
{
     if(summaryAdapter != null) {
        summaryAdapter.clear();
     }

    setListView(month, year, dv);
    summaryList.addAll(Arrays.asList(summary_data));
    summaryAdapter = new SummaryAdapter(this.getActivity().getApplicationContext(), R.layout.listview_item_row, summaryList);


    summaryAdapter.notifyDataSetChanged();
    calendarSummary.setAdapter(summaryAdapter);
}
input
  • 7,503
  • 25
  • 93
  • 150
  • 2
    Some more context on when you're calling adapter.clear() would help. Perhaps you're calling it inside of a thread. I'd post all your code if it's not too long. If it is, just the surrounding code. Also, post the entire error stack trace you see in the logs. – Pzanno Sep 19 '12 at 12:03

1 Answers1

6

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);
}
Community
  • 1
  • 1
Pzanno
  • 2,185
  • 2
  • 20
  • 23
  • Thank you so much for the detailed reply. The only thing now is that when I try to clear the `ArrayList`, it still doesn't clear rather it appends to the previous list. I've updated the question with the code. Please check. – input Sep 19 '12 at 12:38
  • You should still use the adapter.clear() method rather than the list. Is ```adapter.clear()``` still giving you the error? Btw, I edited my answer a few times so you might now have seen the latest yet. – Pzanno Sep 19 '12 at 12:40
  • No, it doesn't give an error now. All it does is now append the previous list data to the current one instead of refreshing/replacing it. – input Sep 19 '12 at 12:43
  • Are you using ```adapter.clear();```? The code you posted above says ```weatherList.clear();``` – Pzanno Sep 19 '12 at 12:47
  • what is the difference between using an array list or an array?? – karn Sep 19 '12 at 12:50
  • Array was throwing the error `UnsupportedOperationException` if I added `adapter.clear()`. If I removed it, the code worked fine but it appended the data to previous list. ArrayList doesn't throw the exception when using `adapter.clear()` but it doesn't clear the data either. – input Sep 19 '12 at 12:54
  • Ah, it's because you are adding to the list itself. ```summaryList.addAll(Arrays.asList(summary_data));``` You should use the adapter to add more items. See the [add](http://developer.android.com/reference/android/widget/ArrayAdapter.html#add(T)) function. – Pzanno Sep 19 '12 at 13:02
  • From what I see the code isn't set up quite right. You should only initialize the ```summaryList``` and the ```summaryAdapter``` once. Then when you want to update it some more you can use the ```summaryAdapter.clear()``` and ```summaryAdapter.add()``` or ```summaryAdapter.remove()``` methods. Try not to manipulate the ```summaryList``` after you have initialized it. If things still don't work you will have to post all the code. – Pzanno Sep 19 '12 at 13:05
  • @Pzanno, what do I add to the adapter? summary_data or summaryList? – input Sep 19 '12 at 13:22
  • After it's initialized you add new ```Weather``` objects. So ```summaryAdapter.add( new Weather(R.drawable.weather_cloudy, "Cloudy") )``` but given the code you have I'll post an updated answer to try and work with what you have. – Pzanno Sep 19 '12 at 13:34