You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX中基于数据模型动态填充ListView的实现问询

Got it, let's wrap up this method and make it clean, maintainable, and fully functional for dynamically populating your ListViews based on the Module data model. Here's a complete, optimized version with explanations:

Complete Implementation

public void setCourseModules(Collection<Module> modules) {
    // Clear all ListViews upfront to avoid duplicate entries
    clearAllListViews();

    // Guard clause to safely handle null/empty input
    if (modules == null || modules.isEmpty()) {
        return;
    }

    modules.forEach(module -> {
        Delivery runPlan = module.getRunPlan();
        if (runPlan == null) {
            // Optional: handle modules with no assigned run plan (log or add to a fallback list)
            return;
        }

        switch (runPlan) {
            case TERM_1:
                populateModuleByMandatoryStatus(module, t1Sel, t1UnSel);
                break;
            case TERM_2:
                populateModuleByMandatoryStatus(module, t2Sel, t2UnSel);
                break;
            case TERM_YEAR: // Assuming this is the enum value for year-long modules (matches your yrSel ListView)
                // Adjust this logic based on your actual requirements for year-long modules
                if (module.isMandatory()) {
                    yrSel.getItems().add(module);
                }
                // Optional: add an unselected year-long list if needed
                break;
            default:
                // Handle unexpected Delivery types (log a warning or ignore)
                break;
        }
    });
}

// Helper method to eliminate duplicate mandatory/optional logic
private void populateModuleByMandatoryStatus(Module module, ListView<Module> selectedList, ListView<Module> unselectedList) {
    if (module.isMandatory()) {
        selectedList.getItems().add(module);
    } else {
        unselectedList.getItems().add(module);
    }
}

// Helper method to clean up repeated clear calls
private void clearAllListViews() {
    t1UnSel.getItems().clear();
    t2UnSel.getItems().clear();
    t1Sel.getItems().clear();
    t2Sel.getItems().clear();
    yrSel.getItems().clear();
}

Key Improvements & Explanations

  • Null Safety: Added guard clauses for null/empty module collections and null runPlan values to prevent unexpected crashes.
  • Code Reuse: Extracted repeated logic (clearing lists, handling mandatory/optional modules) into helper methods. This makes the main method cleaner and easier to update—if you ever change how mandatory modules are handled, you only modify one place.
  • Readability: Replaced nested if-else blocks with a switch statement, which is more intuitive for enum-based branching and easier to extend if you add new Delivery types later (like TERM_SUMMER).
  • Extensibility: The structure makes it trivial to add new ListView handling for additional terms or module types—just add a new case to the switch.

If your year-long module logic needs an unselected list (like the term modules), you can easily adjust the code to use the populateModuleByMandatoryStatus helper method for yrSel too.

内容的提问来源于stack exchange,提问作者dogplasma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:28:22