ObservableList doesn't update ArrayList

丶灬走出姿态 提交于 2019-12-23 16:38:54

问题


For a school assignment, we're working with ObservableList objects from JavaFX (Right?). I've been working on this for over a day now and can't figure it out. The teacher only tells us to 'Google it' so that's no help either..

Basically, we're working on a basic administration application to keep track of people and their families. Where people are member of a family, and a family can have multiple members.

When a person or family is added, they are added to an observableList which should then update an ArrayList (So the data can be serialized) and a GUI element. This is where the problem is.

We currently have the following implementation:

private List<Persoon> personen;
private List<Gezin> gezinnen;
this.personen = new ArrayList<Persoon>();
this.gezinnen = new ArrayList<Gezin>();

private transient ObservableList<Persoon> observablePersonen;
private transient ObservableList<Gezin> observableGezinnen;
observablePersonen = FXCollections.observableArrayList(personen);
observableGezinnen = FXCollections.observableArrayList(gezinnen);

Then when an item is added, we do the following:

Persoon p = new Persoon();
observablePersonen.add(p);
observablePersonen.notifyAll();

After this when we inspect the 'personen' list, the added object isn't there :(

Are we missing something obvious?


回答1:


You need to use FXCollections.observableList instead of FXCollections.observableArrayList.

As per the documentation of observableList:

Constructs an ObservableList that is backed by the specified list.

So any modification of the observable list will be reported to the backing list. However, in the case of observableArrayList:

Creates a new observable array list and adds a content of collection col to it.

so this list is not backed by the given list, it just serves as an initial collection.

As a side note, you should not call notifyAll(): this method has nothing to do with JavaFX and it relates to waking threads waiting on this object.




回答2:


Sample of how an ArrayList can be synced to an ObservableList.

public class Main {

    public static ArrayList<Double> arrayList = new ArrayList();
    public static ObservableList<Double> observableList = FXCollections.observableArrayList();

    public static void main(String[] args) {

        // add a listener to the ObservableList
        observableList.addListener(new ListChangeListener<Double>() {
            @Override
            public void onChanged(Change<? extends Double> c) {
                // c represents the changed element
                System.out.println("Added " + c + " to the Observablelist");
                // we add the last element added to the observable list to the arraylist
                arrayList.add(observableList.get(observableList.size()-1));
                System.out.println("Added " + arrayList.get(arrayList.size()-1) + " to the Arraylist");
            }
        });

        observableList.add(5.0);
        observableList.add(7.0);
    }
}

Output:

Added { [5.0] added at 0 } to the Observablelist
Added 5.0 to the Arraylist
Added { [7.0] added at 1 } to the Observablelist
Added 7.0 to the Arraylist


来源:https://stackoverflow.com/questions/32907813/observablelist-doesnt-update-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!