gpt4 book ai didi

java - 有没有办法将 ObservableList 绑定(bind)到 ObservableMap?

转载 作者:搜寻专家 更新时间:2023-11-01 03:30:48 25 4
gpt4 key购买 nike

我以 ObservableMap<T, Foo> 的格式获取数据.要在 View 中实现此数据,我需要采用 ObservableList<Foo> 格式的 map 值。 .

因为我需要 map 集合中的数据(以避免重复和其他原因),所以我想知道是否可以绑定(bind)这两个集合。

类似于:

ObservableMap<Integer, Foo> data = FXCollections.observableHashMap();
ObservableList<Foo> target = FXCollections.observableArrayList();

// Won't work
Bindings.bindContent(target, data.values());

绑定(bind),因为数据可以在运行时更改。

编辑:使用 map 值初始化列表不起作用,因为将向 map 添加额外的 Foo:以字符串作为映射值的示例:

ObservableMap<Integer, String> map = FXCollections.observableHashMap();
map.put(1, "a");
map.put(2, "b");

ObservableList<String> list = FXCollections.observableArrayList(map.value());

//map = {1=a, 2=b}
//list = [a, b]
// ok

map.put(3, "c");

//map = {1=a, 2=b, 3=c}
//list = [a, b]
// no update

最佳答案

从 ObservableMap 到 ObservableList

ObservableMap<Integer, Foo> data = FXCollections.observableHashMap();
ObservableList<Foo> target = FXCollections.observableArrayList(data.values());

关于java - 有没有办法将 ObservableList 绑定(bind)到 ObservableMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57572988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com