【发布时间】:2018-04-28 07:09:10
【问题描述】:
ListChangeListener.Change.wasUpdated() 用于检测通过ObservableList.observableArrayList(Callback<E,Observable[]> extractor) 创建的ObservableLists 元素内的变化。
如何检索导致更改被触发的确切项目?
编辑
可能问题不够清楚,我举个例子吧。
class Foo {
private StringProperty name = new SimpleStringProperty();
public final StringProperty nameProperty() { return name; }
public final String getName() { return name.get(); }
public final void setName(String n) { name.set(n); }
public Foo(String fooName) { setName(fooName); }
}
// Creates an ObservableList with an extractor
ObservableList<Foo> fooList = FXCollections.observableArrayList(foo -> new Observable[] { foo.nameProperty() });
Foo fooA = new Foo("Hello");
Foo fooB = new Foo("Kitty");
fooList.add(fooA);
fooList.add(fooB);
fooList.addListener(new ListChangeListener<Foo>() {
public void onChanged(Change<Foo> c) {
while (c.next()) {
if (c.wasUpdated()) {
// One or more of the elements in list has/have an internal change, but I have no idea which element(s)!
}
}
}
});
fooB.setName("Mickey");
fooB.setName() 将触发 ListChangeListener 的更改,wasUpdated() 条件将返回 true。但是,我无法知道是 fooB 在侦听器中发生了变化。
这可能看起来微不足道,但我有一个地图应用程序,其中列表存储地图必须呈现的内容。当其中一项更改其位置(即纬度/经度)时,我需要在地图上重新绘制。如果我不知道哪个项目改变了位置,我将不得不重新绘制我已经拥有的所有内容。
【问题讨论】:
-
嗯 ...
change.getList().get(change.getFrom())不工作?
标签: java javafx observablelist