如何通过 ListChangeListener 检索 ObservableList 中的更改项

Jai*_*Jai 3 java javafx observablelist

ListChangeListener.Change.wasUpdated()用于检测ObservableList通过创建的s 的元素内的变化ObservableList.observableArrayList(Callback<E,Observable[]> extractor)

如何检索导致触发更改的确切项目?

编辑

可能这个问题还不够清楚,我举个例子。

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");
Run Code Online (Sandbox Code Playgroud)

fooB.setName()将触发一个变化ListChangeListener,该wasUpdated()条件将返回true。但是,我无法知道它fooB在侦听器中发生了什么变化。

这可能看起来微不足道,但我有一个地图应用程序,其中列表存储了地图必须呈现的内容。当其中一项改变其位置(即纬度/经度)时,我需要在地图上重新绘制。如果我不知道哪个项目改变了位置,我将不得不重新绘制我已经拥有的所有内容。

Jam*_*s_D 5

您可以获得已更改项目的索引,这为您提供了一些有用的信息:

import javafx.beans.Observable;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;

public class ListUpdateTest {

    public static void main(String[] args) {

        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((Change<? extends Foo> c) -> {
             while (c.next()) {
                if (c.wasUpdated()) {
                    int start = c.getFrom() ;
                    int end = c.getTo() ;
                    for (int i = start ; i < end ; i++) {
                        System.out.println("Element at position "+i+" was updated to: " +c.getList().get(i).getName() );
                    }
                }
            }
        });

        fooB.setName("Mickey");
    }

    public static 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); }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您无法从列表更改事件中确定在这些列表元素中更改的实际属性(因此,如果您的提取器指向两个或多个属性,则无法找到哪些属性发生了更改),并且没有获取先前值的方法。不过,这对于您的用例来说可能就足够了。