【问题标题】:UnsupportedOperationException with ArrayAdapter.remove [duplicate]UnsupportedOperationException with ArrayAdapter.remove [重复]
【发布时间】:2011-11-04 05:54:56
【问题描述】:

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

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

【问题讨论】:

    标签: java android adapter


    【解决方案1】:

    当您使用数组初始化 ArrayAdapter 时,似乎会出现此问题。尝试使用List&lt;JournalEntry&gt; 对其进行初始化。参考:Why can't one add/remove items from an ArrayAdapter?

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这不是“最终”的意思。它与 C++ 的“const”不同。
      猜你喜欢
      • 1970-01-01
      • 2015-07-22
      • 2015-12-27
      • 2018-10-20
      • 1970-01-01
      • 2015-10-23
      • 2013-04-01
      • 2019-06-11
      • 2016-10-26
      相关资源
      最近更新 更多