来自ListView.getItems()的可疑行为。removeAll() [英] Suspicious Behavior From ListView.getItems().removeAll()

查看:468
本文介绍了来自ListView.getItems()的可疑行为。removeAll()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含4个对象(不完全相同)的列表,并且我调用JavaFX的 listView.getItems()。removeAll(listView.getSelectionModel()。getSelectedItems())选中一个对象(假设为数字1),对象2和3也将被删除。这是 removeAll()的正常操作吗?它导致了一个相当令人沮丧的错误。这在 removeAll()方法中没有记录,并且相当具有误导性,因为我使用它,因为它比普通的 remove 方法,它使用 Object

解决方案

当您移除选定的项目时,选择将被更新,以便选择其他项目。当选择被更新时,这具有更新由 listView.getSelectionModel()。getSelectedItems()返回的列表的副作用。这与 List.removeAll 方法交互不良。



假设我们有 targetList.removeAll (removeList)。结果是,对于 targetList 中的每个元素,代码会问这个元素是否出现在 removeList ?如果是这样,元素将从 targetList 中移除。这对 removeList 有副作用,它会在下一次循环时改变行为。

更详细地说:
$ b


  1. 这从零开始。由于此元素不出现在 removeList

  2. 接下来是元素1.由于它被选中,所以它出现在 removeList ,因此元素1被删除。 ListView 代码检测到所选元素已被删除,因此它将选择更新为元素2.这意味着 removeList > 现在包含元素2


  3. 接下来是元素2.由于它被选中,所以它出现在 removeList ,所以元素2被删除。 ListView 代码检测到所选元素已被删除,因此它将选择更新为元素3.这意味着 removeList > 现在包含元素3


  4. 我认为您现在可以看到发生了什么。 : - )


可能最简单的方法是在将选择列表传递给 removeAll

  list.getItems()。removeAll(new ArrayList<> 。.getSelectionModel()getSelectedItems())); 


If I have a list containing 4 objects(not all the same), and I call JavaFX'slistView.getItems().removeAll(listView.getSelectionModel().getSelectedItems()) with a single object (let's say number 1) selected, objects 2 and 3 will be removed as well. Is this the normal operation for removeAll()? It led to a rather frustrating bug. This was not documented in the removeAll() method, and is rather misleading because I used it since it has better type-safety than the normal remove method, which takes an Object.

解决方案

When you remove a selected item, the selection will be updated so that some other item will be selected. When the selection is updated, this has the side effect of updating the list returned by listView.getSelectionModel().getSelectedItems(). This interacts badly with the List.removeAll method.

Suppose we have targetList.removeAll(removeList). What happens is that for each element in targetList, the code asks, "does this element appear in removeList?" If it does, the element is removed from targetList. This has a side effect on removeList, which changes the behavior the next time around the loop.

More specifically:

  1. This starts off with element 0. Since this element doesn't appear in removeList, nothing happens.

  2. Next is element 1. Since it's selected, it appears in removeList, so element 1 is removed. The ListView code detects that the selected element has been removed, so it updates the selection to element 2. This means that removeList now contains element 2.

  3. Next is element 2. Since it's selected, it appears in removeList, so element 2 is removed. The ListView code detects that the selected element has been removed, so it updates the selection to element 3. This means that removeList now contains element 3.

  4. I think you can see what's happening now. :-)

Probably the easiest way around this is to copy the selection list before passing it to removeAll:

list.getItems().removeAll(new ArrayList<>(list.getSelectionModel().getSelectedItems()));

这篇关于来自ListView.getItems()的可疑行为。removeAll()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆