1 回答

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
的selectionModelProperty時(shí)候才會(huì)觸發(fā)改變selectionModel被替換這通常不會(huì)發(fā)生。依賴關(guān)系應(yīng)該是sourceList.getSelectionModel().getSelectedItems().
此外,按照我的理解,您應(yīng)該查詢Log'ssource是否在所選項(xiàng)目中,而不是該source字符串是否在所選項(xiàng)目列表中的某個(gè)位置包含字符串的某些部分。
另請(qǐng)注意,ListView應(yīng)避免僅使用 a來存儲(chǔ)數(shù)據(jù),對(duì)于大型列表,contains對(duì) aSet而不是 a進(jìn)行檢查效率更高List。
你可以使用這樣的代碼:
ObjectBinding<Predicate<Log>> binding = new ObjectBinding<Predicate<String>>() {
private final Set<String> strings = new HashSet<>();
{
sourceList.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<String>() {
@Override
public void onChanged(ListChangeListener.Change<? extends String> c) {
boolean changed = false;
// modify set on selection change
while (c.next()) {
if (c.wasRemoved()) {
changed = true;
c.getRemoved().stream().map(String::toLowerCase).forEach(strings::remove);
}
if (c.wasAdded()) {
changed = true;
c.getAddedSubList().stream().map(String::toLowerCase).forEach(strings::add);
}
}
if (changed) {
invalidate();
}
}
});
}
@Override
protected Predicate<Log> computeValue() {
return log -> strings.contains(log.getSource().toLowerCase());
}
};
sourceFilter.bind(binding);
添加回答
舉報(bào)