Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnsupportedOperationException with ArrayAdapter.remove [duplicate]

In my code I have a ListActivity. One of the context menu options for a list item is "delete", which opens a dialog confirming the action. I intended to implement this functionality by first deleting the item's data in the database and then removing it from the ArrayAdapter. It is in removing it from the ArrayAdapter that I get an 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!

like image 272
Prime Avatar asked Jan 18 '23 21:01

Prime


1 Answers

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?

like image 188
Sanjay T. Sharma Avatar answered Jan 30 '23 03:01

Sanjay T. Sharma