【问题标题】:How to remove object from observableArrayList() based on property of said object?如何根据所述对象的属性从 observableArrayList() 中删除对象?
【发布时间】:2020-02-11 19:05:08
【问题描述】:

我有 2 个由 AppointmentCustomer 对象组成的 observableArrayLists,我通过这些对象在应用程序窗口中显示2个表视图。它们是从 SQL 数据库中填充的。

我编写了 deleteCustomer 方法来处理 SQL 操作,最后调用以下方法从列表中删除对象:

            MainScreenController.customerDisplayList.remove(deletedCustomer);

在删除客户之前,我还执行以下代码以删除与已删除客户相关的所有约会,因为约会表包含一个外键列,该列指向客户表中的主键:

    public static void deleteAppointments(int customerId) throws Exception {
    DatabaseConnection.makeConnection();
    String sqlStatement = "DELETE FROM appointment WHERE customerId = '" + customerId + "'";
    Query.makeQuery(sqlStatement);
    DatabaseConnection.closeConnection();
}

现在我的问题是如何根据存储在 Appointment 对象中的 customerId 属性删除 observableArrayList 中包含的约会?

感谢大家的时间和耐心!

【问题讨论】:

    标签: java


    【解决方案1】:

    经过更多研究,我发现可以通过以下代码从 ArrayList 中删除约会:

                MainScreenController.appointmentDisplayList.removeIf(Appointment -> Appointment.getCustomerId() == deletedCustomer.getCustomerId());
    

    【讨论】:

    • Appointment -> Appointment.getCustomerId()。在这两种情况下,appointment 的大写字母在Java 中都是一种糟糕的编码习惯。
    • 太棒了!谢谢!一定会记住这一点。我刚开始大约 2 个月前!
    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    相关资源
    最近更新 更多