首页 > 解决方案 > 这是我的代码中的错误还是 cellfactory 的问题

问题描述

todoitemsListview.setCellFactory(new Callback<ListView<ToDoItems>, ListCell<ToDoItems>>() {
        @Override
        public ListCell<ToDoItems> call(ListView<ToDoItems> toDoItemsListView) {
            ListCell<ToDoItems> cell= new ListCell<ToDoItems>(){
                @Override
                protected void updateItem(ToDoItems items, boolean empty) {
                    super.updateItem(items, empty);
                    if(empty){
                        setText(null);
                    }else {
                        setText(items.getItemName());
                        if (items.getDeadline().equals(LocalDate.now())){
                            System.out.println(items.getDeadline().toString());
                            System.out.println(items.getItemName());
                            setTextFill(Color.RED);
                        }else if (items.getDeadline().equals(LocalDate.now().plusDays(1))){
                            System.out.println(items.getDeadline().toString());
                            System.out.println(items.getItemName());
                            setTextFill(Color.BLUE);
                        }else if (items.getDeadline().equals(LocalDate.now().plusDays(2))){
                            System.out.println(items.getDeadline().toString());
                            System.out.println(items.getItemName());
                            setTextFill(Color.GREEN);
                        }else if(items.getDeadline().isBefore(LocalDate.now())){
                            System.out.println(items.getDeadline().toString());
                            System.out.println(items.getItemName());
                            setTextFill(Color.GREY);
                        }
                    }
                }
            };
            return cell;
        }
    });

我在 listview 中使用了一个 cellfactory,根据它们的截止日期将文本颜色设置为不同的值。但是当我向列表中添加新项目时,不满足 if - else 条件的项目也会突出显示。

这是基于 ObservabelList 的基本 ToDo 应用程序的一部分,其中 ToDoItems 类具有三个属性,即 itemName、ItemDesription、Deadline。

标签: javauser-interfacejavafx

解决方案


a 中的单元格在ListView需要时被重用。因此,在您的情况下,您设置了一个带有红色填充的特定单元格,但是当同一个单元格被重复用于不同的ToDoItems实例时(因为您向下滚动或在列表中添加了一个新项目),并且该新ToDoItems实例没有填充任何改变填充颜色的条件,单元格都会错误地保持其先前的填充颜色。

如果您的示例中没有满足任何条件,您需要做的是确保回退到默认颜色:

todoitemsListview.setCellFactory(new Callback<ListView<ToDoItems>, ListCell<ToDoItems>>() {
    @Override
    public ListCell<ToDoItems> call(ListView<ToDoItems> toDoItemsListView) {
        ListCell<ToDoItems> cell = new ListCell<ToDoItems>() {
            @Override
            protected void updateItem(ToDoItems items, boolean empty) {
                super.updateItem(items, empty);
                if (empty) {
                    setText(null);
                    setTextFill(Color.BLACK);
                } else {
                    setText(items.getItemName());
                    if (items.getDeadline().equals(LocalDate.now())) {
                        System.out.println(items.getDeadline().toString());
                        System.out.println(items.getItemName());
                        setTextFill(Color.RED);
                    } else if (items.getDeadline().equals(LocalDate.now().plusDays(1))) {
                        System.out.println(items.getDeadline().toString());
                        System.out.println(items.getItemName());
                        setTextFill(Color.BLUE);
                    } else if (items.getDeadline().equals(LocalDate.now().plusDays(2))) {
                        System.out.println(items.getDeadline().toString());
                        System.out.println(items.getItemName());
                        setTextFill(Color.GREEN);
                    } else if (items.getDeadline().isBefore(LocalDate.now())) {
                        System.out.println(items.getDeadline().toString());
                        System.out.println(items.getItemName());
                        setTextFill(Color.GREY);
                    } else {
                        setTextFill(Color.BLACK);
                    }
                }
            }
        };
        return cell;
    }
});

推荐阅读