ArrayAdapter.remove 出现 UnsupportedOperationException
在我的代码中,我有一个 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();
感谢任何帮助。 谢谢!
In my code I have a ListActivity
. One of the context menu options for a list item is "delete", which opens a dialog confirming the action. I intended to implement this functionality by first deleting the item's data in the database and then removing it from the ArrayAdapter
. It is in removing it from the ArrayAdapter
that I get an 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();
Any help appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用数组初始化 ArrayAdapter 时,似乎会出现此问题。尝试使用
List
对其进行初始化。参考:为什么不能从 ArrayAdapter 添加/删除项目?It seems that this problem crops up when you initialize your
ArrayAdapter
with an array. Try initializing it with aList<JournalEntry>
. Reference: Why can't one add/remove items from an ArrayAdapter?您正在尝试修改声明为
final
的列表。编译器试图警告您,但您已通过@SuppressWarnings("unchecked") 抑制了警告
You're trying to modify list which is declared as
final
. Compiler tried to warn you, but you've suppressed warning by@SuppressWarnings("unchecked")