ArrayAdapter.remove 出现 UnsupportedOperationException

发布于 2024-12-01 06:59:16 字数 978 浏览 3 评论 0原文

在我的代码中,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2024-12-08 06:59:16

当您使用数组初始化 ArrayAdapter 时,似乎会出现此问题。尝试使用 List 对其进行初始化。参考:为什么不能从 ArrayAdapter 添加/删除项目?

It seems that this problem crops up when you initialize your ArrayAdapter with an array. Try initializing it with a List<JournalEntry>. Reference: Why can't one add/remove items from an ArrayAdapter?

慵挽 2024-12-08 06:59:16

您正在尝试修改声明为 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")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文