如何从ListView完全编辑/删除元素? [英] How do I completely edit/delete an element from ListView?

查看:121
本文介绍了如何从ListView完全编辑/删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与该问题有关的信息:

Information related to the problem:

我有一个类( Shell ),其中包含一个任务图,声明为:

I have a class (Shell) that holds a Map of tasks, declared as:

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;
}

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

My application allows the user to create, edit or delete a Task.

创作:

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)方法,该方法执行以下操作:

Above, I use the loadTask(name) method, which does the following:

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

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

Finally, my ListView javafx object is declared as:

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());                
            }
        });
    }

问题本身:

Problem itself:

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

When I create a Task, my ListView is automatically updated and it is added to the list. - great

但是,我只是不明白为什么,当我编辑任务时,我看到它已成功更改,但是我的ListView对象没有刷新/更新列表.重新启动程序后,我的ListView似乎已更新. -为什么会这样?

However, and I just can't understand why, when I edit a task I see that it has successfully changed but my ListView object doesn't refresh/update the list. As soon as I restart my program, my ListView appears updated. - Why is this happening?

此外,我无法删除任何任务.当我调用deleteTask方法时,系统会打印已删除任务",但是我仍然可以在listView上选择它并对其进行编辑,并将其与对象一起保存,以便在我加载它时保留已删除"任务.

Plus, I can't delete any task. When I call the deleteTask method, System prints "Task deleted" but I can still select it on listView and edit it and it is saved with the object so that when I load it the "deleted" task remains.

推荐答案

使用ObservableList代替List

Use ObservableList instead of List

来自JavaFX文档

此处显示了如何创建和填充名称(字符串)的ListView的简单示例:

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

ObservableList名称= FXCollections.observableArrayList( "Julia","Ian","Sue","Matthew","Hannah","Stephan","Denise"); ListView listView =新的ListView(名称); ListView的元素包含在ObservableList项目中. ListView会自动观察此ObservableList,因此ObservableList内部发生的任何更改都将自动显示在ListView本身中.如果将ObservableList传递到ListView构造函数中不可行,则建议的设置项目的方法是简单地调用:

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内容= ... listView.setItems(content); 如上所述,这样做的最终结果是ListView将自动刷新视图以表示列表中的项目. 虽然ListView接受了另一种方法,但不是推荐的方法:

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:

列表内容= ... getItems().setAll(content); 上面显示的方法的问题在于,内容列表正在复制到项目列表中-这意味着将不会观察到对内容列表的后续更改,并且这些内容也不会在ListView中直观地反映出来.

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.

这篇关于如何从ListView完全编辑/删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆