我正在尝试从vaadin listbox元素中选择一个项。我用数据库中对象的数组列表填充列表框。选择对象/列表项后,我想用所选对象的属性填充文本字段。这是我目前的代码。我´我试了那么多,但是我能´无法正常工作:/
// creating a ArrayList - listOfItems - filled with Items from the Database
listBox.setItems(listOfItems);
listBox.setHeight("100px");
add(listBox);
Div value = new Div();
listBox.addValueChangeListener(event -> {
if (event.getValue() == null) {
Notification.show(event.getValue().toString());
} else {
Notification.show("value is null");
}
});
有人知道为什么吗?
提前谢谢
2条答案
按热度按时间uqxowvwt1#
你可以用
.setValue(...)
listbox类的方法。但是,当您从数据库加载数据时,您必须确保您选择的项目与您通过提供的项目完全相同.setItems(...)
方法。这意味着,其中一个提供的项必须与要选择的项具有完全相同的哈希代码。否则您的选择可能不起作用。对于一些示例,请看:https://vaadin.com/components/vaadin-list-box/java-examples
cnjp1d6j2#
这里有一个bug:
在第一部分
if
声明,你打电话来event.getValue().toString()
,这将导致空指针异常,如event.getValue()
为空。所以把条件转到if (event.getValue() != null)