我有 2 个 observableArrayList,由我在应用程序窗口中显示的 Appointment 和 Customer 对象组成2 个 TableView 。它们是从 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 中包含的 Appointments?
感谢大家的时间和耐心!
最佳答案
经过更多研究,我发现可以通过以下代码从 ArrayList 中删除约会:
MainScreenController.appointmentDisplayList.removeIf(Appointment -> Appointment.getCustomerId() == deletedCustomer.getCustomerId());
关于java - 如何根据对象的属性从 observableArrayList() 中删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60175771/