gpt4 book ai didi

java - 如何从 ListView 中完全编辑/删除元素?

转载 作者:行者123 更新时间:2023-12-01 11:13:58 25 4
gpt4 key购买 nike

与问题相关的信息:

我有一个类(Shell),它保存任务映射,声明为:

private Map<String, Task> _tabs = new HashMap<String,Task>();
private List<Task> _tsks; //this is the result of the function below:

public List<Task> MapToArray() {

this._tsks = new ArrayList<Task>();
for(String key : _tabs.keySet()) {
this._tsks.add(_tabs.get(key));
}
return this._tsks;
}

我的应用程序允许用户创建、编辑或删除任务。

创作:

shl.createTab(taskName.getText(), taskDescription.getText(), taskAssignee.getText()); //shl is the *Shell* class. 

版本:

public void editTask(String name, String description, String assignee) {
this.deleteTask(loadTask(name));
_currentTab.setAssignee(findUser(assignee));
_currentTab.setDescription(description);
_currentTab.setName(name);
this.setEmployees(this.getEmployees());
this.setManagers(this.getManagers());
this.setTabs(this.getTabs());
this.setUsers(this.getUsers());

}

删除:

public void deleteTask(Task currentTab) {
String exName = this.getCurrentTab().getName();
this._tabs.remove(loadTask(exName));
this._tabs.entrySet().remove(exName);
this._tsks.remove(loadTask(exName));

System.out.println("Task '"+ exName + "' deleted");

}

上面,我使用了 loadTask(name) 方法,该方法执行以下操作:

public Task loadTask(String tabName) {
return this._tabs.get(tabName);
}

最后,我的 ListView javafx 对象声明为:

private ListView<Task> lstView;

填充它的方法是:

    public void refreshList() {

lstView.setItems(FXCollections.observableArrayList(shl.MapToArray())); //once again, shl is the *Shell* class
lstView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
lstView.setOnMouseClicked(new EventHandler<MouseEvent>() {

@Override
public void handle(MouseEvent event) {
System.out.println("clicked on " + lstView.getSelectionModel().getSelectedItem());
shl.setCurrentTab((Task) lstView.getSelectionModel().getSelectedItem());
taskName.setText(shl.getCurrentTab().getName());
description.setText(shl.getCurrentTaskDescription());
name.setText(shl.getCurrentTab().getAssignee());
comment.setText(shl.getCurrentTab().getComment());
}
});
}

问题本身:

当我创建任务时,我的 ListView 会自动更新并将其添加到列表中。 - 太棒了

但是,我就是不明白为什么,当我编辑任务时,我看到它已成功更改我的 ListView 对象没有刷新/更新列表。一旦我重新启动程序,我的 ListView 就会显示更新。 - 为什么会发生这种情况?

另外,我无法删除任何任务。当我调用deleteTask方法时,系统打印“任务已删除”,但我仍然可以在listView上选择它并编辑它,它与对象一起保存,以便当我加载它时,“已删除”任务仍然存在。

最佳答案

使用 ObservableList 而不是 List

来自 JavaFX 文档

A simple example of how to create and populate a ListView of names (Strings) is shown here:

ObservableList names = FXCollections.observableArrayList( "Julia", "Ian", "Sue", "Matthew", "Hannah", "Stephan", "Denise"); ListView listView = new ListView(names); The elements of the ListView are contained within the items ObservableList. This ObservableList is automatically observed by the ListView, such that any changes that occur inside the ObservableList will be automatically shown in the ListView itself. If passying the ObservableList in to the ListView constructor is not feasible, the recommended approach for setting the items is to simply call:

ObservableList content = ... listView.setItems(content); The end result of this is, as noted above, that the ListView will automatically refresh the view to represent the items in the list. Another approach, whilst accepted by the ListView, is not the recommended approach:

List content = ... getItems().setAll(content); The issue with the approach shown above is that the content list is being copied into the items list - meaning that subsequent changes to the content list are not observed, and will not be reflected visually within the ListView.

关于java - 如何从 ListView 中完全编辑/删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32053193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com