我试图用javafx将元素放在listview和treeview上,但是两个控件都不会刷新它们的内容。我使用一个可观察的列表来控制项目,并且每次删除一个项目时,listview或treeview都会将其从数据源中删除。但视图未更新。我仍然看到所有物品。唯一的区别是,已删除的项目无法再选择。例如,链接2显示了折叠的项目列表。图1显示了折叠之前的项目。这些项目已折叠,但旧条目仍然可见。有人知道这个问题的解决方案吗?谢谢大家对我的帮助链接1:treeview未折叠 链接2:treeview已折叠但未更新旧视图这是我用来显示列表视图的自定义单元工厂:public ListCell<T> call(final ListView<T> param) { ListCell<T> cell = new ListCell<T>(){ @Override protected void updateItem(final T persistentObject, final boolean empty) { super.updateItem(persistentObject, empty); if(persistentObject instanceof POProcessStep){ POProcessStep poProcessStep = (POProcessStep) persistentObject; if (persistentObject != null) { super.setText(poProcessStep.getId() + " - " + poProcessStep.getTitle()); } }else if(persistentObject instanceof POProcess){ POProcess poProcess = (POProcess) persistentObject; if (persistentObject != null) { super.setText(poProcess.getId() + " - " + poProcess.getTitle()); } }else if(persistentObject instanceof POCategory){ POCategory poCategory = (POCategory) persistentObject; if(persistentObject != null){ super.setText(poCategory.getId() + " - " + poCategory.getTitle()); } }else if(persistentObject instanceof String){ if(persistentObject != null){ super.setText(String.valueOf(persistentObject)); } } super.setGraphic(null); } }; return cell; }
1 回答

九州编程
TA贡献1785条经验 获得超4个赞
您的单元工厂updateItem(...)需要处理单元为空的情况。这就是删除某项(或由于TreeView折叠了一个节点而将其变为空)并将先前显示该项目的单元格重用为空单元格的情况:
public ListCell<T> call(final ListView<T> param) {
ListCell<T> cell = new ListCell<T>(){
@Override
protected void updateItem(final T persistentObject, final boolean empty) {
super.updateItem(persistentObject, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
// ... rest of your code.
}
}
}
return cell ;
}
添加回答
举报
0/150
提交
取消