UnsupportedOperationException异常与ArrayAdapter.remove异常、UnsupportedOperationException、remove、ArrayAdap

由网友(杯酒困英雄)分享简介:在我的code我有一个 ListActivity 。其中的列表项的上下文菜单选项是删除,这将打开一个对话框,确认操作。我打算先删除数据库中的项目的数据,然后从 ArrayAdapter 卸下来实现此功能。它是从取出的 ArrayAdapter ,我得到一个 UnsupportedOperationException异常...

在我的code我有一个 ListActivity 。其中的列表项的上下文菜单选项是删除,这将打开一个对话框,确认操作。我打算先删除数据库中的项目的数据,然后从 ArrayAdapter 卸下来实现此功能。它是从取出的 ArrayAdapter ,我得到一个 UnsupportedOperationException异常 ...

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();

任何帮助AP preciated。 谢谢!

Any help appreciated. Thanks!

推荐答案

看来,这个问题也出现在初始化的 ArrayAdapter 一个数组。尝试用对其进行初始化列表&LT; JournalEntry的&GT; 。参考:Why不能在一个加/从ArrayAdapter删除项目?

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?

阅读全文

相关推荐

最新文章