使用ArrayAdapter.remove时出现UnsupportedOperationException错误

4
在我的代码中,我有一个 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个回答

10

-1

你试图修改一个被声明为final的列表。编译器试图警告你,但你通过@SuppressWarnings("unchecked")抑制了警告。


这不是“final”所表示的意思。它与C++中的“const”不同。 - mhsmith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接