我正在尝试使用来自HashMap的TreeItems填充我的TreeView。一些数据带有属性“isProject”,这意味着它是某个客户的项目。我正在尝试将这些Project节点添加到更高的客户端节点......但是建立一个糟糕的反馈循环。是否有更好的方法来获取特定节点(TreeItem)并为其添加另一个节点?还是一种使这段代码有效的方法?谢谢!
@SuppressWarnings({ "rawtypes", "unchecked" })
private void showTimeRecords() {
ArrayList<TimeRecord> timeRecords = new ArrayList<>();
ObservableList<PerformanceLogTableData> pflData = FXCollections.observableArrayList();
TimeDataRecords rootValue = new TimeDataRecords("Client / Project", "", "", false, timeRecords, pflData);
YearMonth setYM = YearMonth.of(2016, currMonth);
root = new TreeItem<TimeDataRecords>();
HashMap<String,TimeDataRecords> timeDataRecsMap = timeDataRecsBigMap.get(setYM);
for (HashMap.Entry<String, TimeDataRecords> entry : timeDataRecsMap.entrySet()) {
TreeItem<TimeDataRecords> node = new TreeItem<TimeDataRecords>(entry.getValue());
if (entry.getValue().isProject()) {
for (int i = 0; i < root.getChildren().size(); i++) {
if(root.getChildren().get(i).getValue().getName().equals(entry.getValue().getName())){
TreeItem<TimeDataRecords> tempNode = new TreeItem<TimeDataRecords>(root.getChildren().get(i).getValue());
tempNode.getChildren().add(node);
root.getChildren().add(tempNode);
}
}
}else{
root.getChildren().add(node);
System.out.println(entry.getValue().getId() + " --- " + entry.getValue().getProject() + " --- " + entry.getValue().isProject());
}
}
root.setValue(rootValue);
root.setExpanded(true);
treeView.setRoot(root);
treeView.setEditable(false);
}
答案 0 :(得分:0)
首先尝试在LinkedList中预先添加项目数据。
LinkedList<TreeItem<TimeDataRecords>> entrys = new LinkedList<>();
for(TimeDataRecords entry : timeDataRecsMap.values()){
TreeItem<TimeDataRecords> node = new TreeItem<TimeDataRecords>(entry);
if(entry.isProject()) entrys.addFirst(node);
else entrys.addLast(node);
}
root.getChildren().addAll(entrys);