Reputation: 2811
I'm not able to update my javafx.scene.control.ListView<Todo>
control.
As you can see the listview displays the headline property.
When i change the headline of a todo the list-view still displays the old value.
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");
I'm missing a method like listview.refresh(), or update()
So what's the right way to do this?
Edit: I find a way of doing this, but i would be glad, if someone can explain the "official" way of doing this.
void updateListView(Todo todo) {
int index = this.listView.getItems().indexOf(todo);
this.listView.getItems().remove(todo);
this.listView.getItems().add(index, todo);
}
Upvotes: 0
Views: 201
Reputation: 2639
I found a workaround. It is not really beautifull but it does the job. I'll still dig for a better way to do it...
int index = listView.getSelectionModel().getSelectedIndex();
System.out.println(listView.getItems());
ObservableList<Todo> olist = listView.getItems();
olist.get(index).setHeadline("new");
listView.setItems(null);
listView.setItems(olist);
OR
listView.getSelectionModel().getSelectedItem().setHeadline("new 2");
ObservableList<Todo> olist = listView.getItems();
listView.setItems(null);
listView.setItems(olist);
Here's the "better" way
Make a little change in your Todo class so that you have a getHeadline()
method that returns a StringProperty instead of a string.
Then, make your own implementation of the CellFactory and bind to textProperty the StringProperty given by the getHeadline() method :
listView.setCellFactory(new Callback<ListView<Todo>, ListCell<Todo>>() {
public ListCell<Todo> call(ListView<Todo> param) {
final ListCell<Todo> cell = new ListCell<Todo>() {
@Override
public void updateItem(Todo item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
textProperty().bind(item.getHeadline());
}
}
};
return cell;
}
});
Now when you make a change to the underlying list the corresponding cell is updated
Upvotes: 1