UnsupportedOperationException with ArrayAdapter.remove

There is ListActivity in my code. One of the context menu options for the list item is "delete", which opens a dialog box confirming the action. I intended to implement this functionality by first deleting the item data in the database and then deleting it from the ArrayAdapter . This is removing it from the ArrayAdapter , which I get a UnsupportedOperationException ...

 public void onClick(DialogInterface dialog, int id) { asynchronousDeleteEntry(CONTEXT_SELECTED_ID); dialog.dismiss(); //I -know- that the adapter will always be an object //of ArrayAdapter<JournalEntry> because this is the only type //I ever call setListAdapter with. Debugging confirms this @SuppressWarnings("unchecked") final ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>) journalViewerListActivity.this.getListAdapter(); //EXCEPTION OCCURS HERE adapter.remove(adapter.getItem(CONTEXT_SELECTED_POSITION)); //refreshes the ListView to show the new items adapter.notifyDataSetChanged(); 

Any help appreciated. Thanks!

+4
source share
2 answers

It seems that this problem occurs when you initialize your ArrayAdapter array. Try initializing it with List<JournalEntry> . Link: Why can't I add / remove elements from the ArrayAdapter?

+8
source

You are trying to modify a list declared as final . The compiler tried to warn you, but you suppressed the warning @SuppressWarnings("unchecked")

-1
source

Source: https://habr.com/ru/post/1369104/


All Articles