从ListView奇怪的行为中删除项目 [英] Removing items from ListView strange behaviour

查看:121
本文介绍了从ListView奇怪的行为中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ListView。添加一个新项目到ListView很容易,但如何删除一个?

I am working with a ListView. Adding a new item to a ListView is easy, but how to remove one?

我以这种方式构建我的ListView:

I build my ListView this way :

public Node buildListView() {
        ObservableList<String> myList =  FXCollections.observableArrayList();
        int counter = 0;
        for(int i=0; i<10; i++) {
            myList.add("Number " + new Integer(i+1).toString());
        }
        ListView<String> listView = new ListView<>();
        listView.setItems(myList);

        listView.setCellFactory(new Callback<ListView<String>, ListCell<String>> (){
            @Override
        public ListCell<String> call(ListView<String> arg0) {
                return new listcells.ListcellY(listView);

            }

        });
        return listView;
}

ListcellY的实例在setGraphics中设置标签。要从我的ObservableList中删除与其关联的String,我添加一个MouseEvent处理程序:

An instance of ListcellY sets a label in setGraphics. To remove the String associated to it from my ObservableList I add a MouseEvent handler :

public class ListcellY extends ListCell<String> {
    Label label;
    HBox hbox;
    ListView<String> listView;

    public ListcellY( ListView<String> listView ) {
        super();
        this.listView = listView; // Here I pass the reference to my list. Is it a gd way to do that? 
    }
    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if(item!=null){
            label = new Label(item);

            label.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> { 
                System.out.println("CLicked on : " + item);
                for(String s : listView.getItems()) {
                    System.out.println("  " + s);
                }

                listView.getItems().remove(item);  // Here I remove the item form my list.
                System.out.println("list size : " + listView.getItems().size());
            });
            setGraphic(label);

        } 

    }
}

虽然此代码通过从我的ObservableList中删除每个单击的项目来工作,但listView的行为却很奇怪。每次单击一个标签时,它都会从我的ListView中成功消失,但是在该ListView的末尾会自动添加一个新项目,从而标出最后一个标签文本的名称(但它不是可点击的标签)。
它一直重复,直到我的ObservableList为空。然后ListView没有显示任何内容。

While this code works by removing each clicked item from my ObservableList, the listView is acting strangely. Each time a label is clicked, it successully disappears from my ListView, however a new item is automatically added at the end of that ListView, baring the name of the last label's text (but it is not a clickable label). It keeps repeating until my ObservableList is empty. Then the ListView does not show anything.

我只想让我点击的标签消失,而不会在我的TreeView中获得任何新的可视化副本。如果索引参数设置为我的ObservableList的原始大小,则ListView会复制最后一个元素,直到它获得正确数量的元素。但我不明白它是如何完成的。

I simply want my clicked labels to disappear, without getting any new "visual copy" in my TreeView. It is s if an index parameter is set to the original size of my ObservableList, then the ListView copies the last element until it gets the right number of elements. But I don't understand how it is done.

我能做什么,首先发生了什么?

What can I do, what's going on in the first place?

推荐答案

好吧,好像写你的问题让你的大脑更好!我得到了正确的直觉:
在我的扩展ListCell类中,我检查重写更新方法我的项目是否为空。但是当它为null时我不处理这种情况,在这种情况下我必须将图形设置为null!我仍然不明白为什么,但它的确有效!
这是更新的类:

Okay, seems like writing your issues makes your brain work better! I got the right intuition : In my extending ListCell class, I check in the overriden update method whether my item is not null. but I don't handle the case when it is null, in which case I have to set the graphics to null! I still don't understand why exactly, but it works! Here is the updated class :

public class ListcellY extends ListCell<String> {
    Label label;
    HBox hbox;
    ListView<String> listView;

    public ListcellY( ListView<String> listView ) {
        super();
        this.listView = listView; // Here I pass the reference to my list. Is it a gd way to do that? 
    }
    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if(item!=null){
            label = new Label(item);

            label.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> { 
                System.out.println("CLicked on : " + item);
                for(String s : listView.getItems()) {
                    System.out.println("  " + s);
                }

                listView.getItems().remove(item);  // Here I remove the item form my list.
                System.out.println("list size : " + listView.getItems().size());
            });
            setGraphic(label);

        }
        else {
            setGraphics(null);
        } 

    }
}

这篇关于从ListView奇怪的行为中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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