observableList不在循环中显示行吗?

时间:2019-01-27 10:26:20

标签: javafx observablelist

enter image description here

我遇到了一个问题,我要闲逛几天才能在循环中的可观察列表中显示行!但是代码在控制台中的功能非常好!任何帮助都将受到欢迎。

 private void showActivitiesDriver() throws IOException {
            Path path = Paths.get("Download/Cards/F__100000015956101711071539.C1B");
            byte[] data = Files.readAllBytes(path);
            dataFileCard = new FileBlockTGD(data);
            w = new WrapperActivityChangeInfo(dataFileCard.getDriver_activity_data()
                    .getActivityDailyRecords().get(i), dataFileCard.getDriver_activity_data()
                    .getActivityDailyRecords().get(i).getActivityChangeInfo().get(j));

            for (i = 0; i < dataFileCard.getDriver_activity_data().getActivityDailyRecords().size(); i++) {

                w.setTheDateTime(dataFileCard.getDriver_activity_data()
                        .getActivityDailyRecords().get(i).getActivityRecordDate().toString());

                System.out.println(w.getTheDateTime());

                dateActivitiesColumn.setCellValueFactory(cellData -> new SimpleStringProperty(
                        cellData.getValue().getTheDateTime()));

                /*
                dateActivitiesColumn.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(
                        DateUtil.parseDateToString(cellData.getValue().getActivityRecordDate())));
                */       

                for (j = 0; j < dataFileCard.getDriver_activity_data().getActivityDailyRecords().get(i).
                        getActivityChangeInfo().size(); j++) {

                    driverActivitiesColumn.setCellValueFactory(cellData -> new SimpleStringProperty(
                            cellData.getValue().getTheActivity()));

                    WrapperActivityChangeInfoList = FXCollections.observableArrayList();

                    WrapperActivityChangeInfoList.add(new WrapperActivityChangeInfo(dataFileCard.getDriver_activity_data()
                            .getActivityDailyRecords().get(i), dataFileCard.getDriver_activity_data()
                            .getActivityDailyRecords().get(i).getActivityChangeInfo().get(j)));
                    System.out.println(WrapperActivityChangeInfoList);
                }

            }
            // Set data to tableview
            activitiesDriverTableView.setItems(WrapperActivityChangeInfoList);
            //System.out.println(WrapperActivityChangeInfoList.toString());
        }
    }

我希望看到所有的行,但是只有一行,但是代码在控制台中很好用。

1 个答案:

答案 0 :(得分:1)

您可以在内循环的每次迭代中创建一个新的ObservableList(最初为空)。这样,在add调用之后,您得到了一个包含单个列表的列表。最后,这些列表之一被分配给TableView。在输出中应该看到一个日期序列,后面跟随着许多WrapperActivityChangeInfo.toString结果,[] 包围,表明您要打印多个列表。

此外,在循环的每次迭代中都没有必要分配cellValueFactory

您需要将列表创建移出循环。

...

WrapperActivityChangeInfoList = FXCollections.observableArrayList();

driverActivitiesColumn.setCellValueFactory(cellData -> new SimpleStringProperty(
                cellData.getValue().getTheActivity()));

for (i = 0; i < dataFileCard.getDriver_activity_data().getActivityDailyRecords().size(); i++) {

    ...       

    for (j = 0; j < dataFileCard.getDriver_activity_data().getActivityDailyRecords().get(i).
            getActivityChangeInfo().size(); j++) {

        WrapperActivityChangeInfoList.add(new WrapperActivityChangeInfo(dataFileCard.getDriver_activity_data()
                .getActivityDailyRecords().get(i), dataFileCard.getDriver_activity_data()
                .getActivityDailyRecords().get(i).getActivityChangeInfo().get(j)));
        System.out.println(WrapperActivityChangeInfoList);
    }

}
activitiesDriverTableView.setItems(WrapperActivityChangeInfoList);