小编典典

ArrayAdapter.remove出现UnsupportedOperationException

java

在我的代码中,我有一个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();

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


阅读 243

收藏
2020-09-28

共1个答案

小编典典

当您ArrayAdapter使用数组初始化时,似乎出现了此问题。尝试使用初始化它List<JournalEntry>。参考:为什么一个人不能从ArrayAdapter中添加/删除项目?

2020-09-28