java - 这是我的代码中的错误还是 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。
解决方案
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;
}
});
推荐阅读
- azure - 如何正确配置 Azure 应用程序网关重写 URL 规则?
- sql-server - 左连接后 SQL SERVER 更新或插入
- r - 如何在只有一个初始值的变异中使用滞后/领先?
- php - Jquery滚动一次加载所有行,一次只能加载5个
- firebase - kotlin 发生 Firebase StorageException
- python - 在 Python 中读取 Excel 图表
- visual-studio - 访问冲突写入位置 0x00000000 在 for 循环中间
- python - 如何从我的 views.py 函数将超过 1 张图像发送到我的模板?
- reactjs - 无法通过使用 Reactjs 单击图标来显示菜单项
- javascript - 从函数中使用 POST