UnsupportedOperationException与ArrayAdapter.remove
问题描述:
在我的代码中我有一个ListActivity
。列表项的其中一个上下文菜单选项是“删除”,它将打开一个对话框,确认该操作。我打算通过首先删除数据库中的项目数据然后从ArrayAdapter
中删除它来实现此功能。它是从ArrayAdapter
删除它,我得到了UnsupportedOperationException
...UnsupportedOperationException与ArrayAdapter.remove
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();
赞赏任何帮助。 谢谢!
答
看起来,这个问题在您用数组初始化ArrayAdapter
时突然出现。尝试使用List<JournalEntry>
进行初始化。参考:Why can't one add/remove items from an ArrayAdapter?
答
您正在尝试修改声明为final
的列表。编译器试图警告你,但你已经禁止了警告@SuppressWarnings("unchecked")
这不是“最终”的意思。它与C++“const”不一样。 – mhsmith