My datasource provides an ObservableList<String>, however for my ListView I need an ObservableList<Warning>.
A Warning is basically just a decorator for the strings, adding a boolean to provide a means for tracking the state of checkboxes my ListView, as proposed in this answer.
class Warning {
private final ReadOnlyStringWrapper name;
private final BooleanProperty checked;
/* ... */
}
Currently I am observing change-events in the original list and add/remove items in the warnings list manually:
ObservableList<String> stringList = ...;
ObservableList<Warning> warningList = ...;
stringList.addListener(new ListChangeListener<String>() {
@Override
public void onChanged(ListChangeListener.Change<? extends String> change) {
if (change.wasAdded()) {
warningList.addAll(change.getAddedSubList().stream().map(Warning::new).collect(Collectors.toList()));
} else if (change.wasRemoved()) {
change.getRemoved().forEach(str -> {
warningList.removeIf(w -> str.equals(w.name));
});
}
}
});
My question is: Is there a more elegant way to decorate my String-typed list, so it can be used as a Warning-typed list without manually passing through change events?
To be more precise: If a string is added to or removed from the original list, I want to see this change immediately in the Warnings-list and thus the ListView.
Aucun commentaire:
Enregistrer un commentaire