猿问

过滤列表无法使用 .contains

我正在尝试将我的列表视图转换为过滤后的列表视图。我找到了关于堆栈溢出的说明,但是线程被锁定了,所以我不能评论它来问这个问题。出于某种原因,当我键入表达式时:


filteredOblist.setPredicate(s -> s.contains(filterUID));

s.contains(filteredUID)); 由于错误而无法工作


错误:(65、51)java:找不到符号符号:方法包含(java.lang.String)位置:Controllers.ModelTable类型的变量s


完整代码:


 public void initialize(URL location, ResourceBundle resources) {

        filter_uid.textProperty().addListener(obs ->{

            String filterUID = filter_uid.getText();

            if (filterUID == null || filterUID.length() == 0){

                filteredOblist.setPredicate(s -> true);

            }else {

                filteredOblist.setPredicate(s -> s.contains(filterUID));

            }

        });

定义:


@FXML public TableView<ModelTable> taskManagerView;

    @FXML public TableColumn<ModelTable, String> col_UID;

    @FXML public TableColumn<ModelTable, String> col_Date;

    @FXML public TableColumn<ModelTable, String> col_CreatedBy;

    @FXML public TableColumn<ModelTable, String> col_Category;

    @FXML public TableColumn<ModelTable, String> col_Task;


        @FXML

        TextField filter_uid;

        @FXML

        TextField filter_created;

        @FXML

        ChoiceBox filtered_choice;

        @FXML

        DatePicker filter_date;




    ObservableList<ModelTable> oblist = FXCollections.observableArrayList();

    FilteredList<ModelTable> filteredOblist = new FilteredList<>(oblist, s -> true);

谢谢


陪伴而非守候
浏览 204回答 1
1回答

斯蒂芬大帝

为此,您的ModelTable班级应该有contains这样的方法,public boolean contains(String uuid) {&nbsp; &nbsp; &nbsp; &nbsp; return UID.equals(uuid);}相反,如果您需要如评论中所述采用不同的过滤策略,我会将该责任移交给客户,只需将其从ModelTable课堂上移除即可。只需通过根据需要访问不同的字段来编写谓词ModelTable。这是从类中删除contains方法后两个谓词的样子。ModelTablefilteredOblist.setPredicate(s -> s.UID.equals(filterUID));filteredOblist.setPredicate(s -> s.CreatedBy.startsWith(filterCreated));底线是您的客户端必须提供过滤策略,因为它会根据上下文而变化。
随时随地看视频慕课网APP

相关分类

Java
我要回答