我无法更新我的javafx.scene.control.ListView<Todo>
控件。如您所见,列表视图显示了标题属性。当我更改待办事项的标题时,列表视图仍显示旧值。
public class Todo {
private StringProperty headline = new SimpleStringProperty();
private StringProperty description = new SimpleStringProperty();
public Todo(String aHeadline) {
this.setHeadline(aHeadline);
}
public Todo(String aHeadline, String aDescription) {
this(aHeadline);
this.description.set(aDescription);
}
public String getDescription() {
return this.description.get();
}
public StringProperty descriptionProperty() {
return this.description;
}
public void setDescription(String aDescription) {
this.description.set(aDescription);
}
public String getHeadline() {
return this.headline.get();
}
public void setHeadline(String aHeadline) {
this.headline.set(aHeadline);
}
public static ObservableList<Todo> getMockups() {
try {
return FXCollections.observableArrayList(TodoXmlParser.getTodosFromXml(Paths.get("/Todos.xml")));
} catch (JDOMException | IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public String toString() {
return this.headline.get();
}
}
this.listView = new ListView<>();
this.listView.setItems(Todo.getMockups());
this.listView.getSelectionModel().getSelectedItem().setHeadline("new");
我错过了这样的方法listview.refresh(), or update()
那么正确的方法是什么?
编辑:我找到了这样做的方法,但如果有人可以解释这样做的“官方”方式,我会很高兴。
void updateListView(Todo todo) {
int index = this.listView.getItems().indexOf(todo);
this.listView.getItems().remove(todo);
this.listView.getItems().add(index, todo);
}