ArrayAdapter.remove支持UnsupportedOperationException

时间:2011-08-26 05:25:07

标签: java android adapter

在我的代码中,我有一个ListActivity。列表项的上下文菜单选项之一是“删除”,这将打开确认操作的对话框。我打算通过首先删除数据库中的项目数据然后从ArrayAdapter中删除它来实现此功能。它是从ArrayAdapter中删除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();

任何帮助表示赞赏。 谢谢!

2 个答案:

答案 0 :(得分:8)

使用数组初始化ArrayAdapter时,似乎会出现此问题。尝试使用List<JournalEntry>初始化它。参考:Why can't one add/remove items from an ArrayAdapter?

答案 1 :(得分:-1)

您正在尝试修改声明为final的列表。编译器试图警告你,但你已经通过@SuppressWarnings("unchecked")

来抑制警告