java: 找不到适合 addAll(java.util.List<java.lang.String>) 的方法

java: no suitable method found for addAll(java.util.List<java.lang.String>)

我想传递一个数组列表。我认为错误是由搜索列表引起的。我知道 addAll 方法适用于集合。这就是为什么它说我的搜索列表无法转换为集合,我该如何解决这个问题This is the error

我的 seacrh 控制器

@FXML
private ListView<?> ListView;

@FXML
private TextField searchbar;

@FXML
private Button searchbtn;

HelloController hc = new HelloController();


public  void  initialize(URL url, ResourceBundle resourceBundle){
    ListView.getItems().addAll();
}

public void searchbtn(ActionEvent actionEvent) throws IOException {
    ListView.getItems().clear();
    ListView.getItems().addAll(hc.searchList(searchbar.getText(),blxckie));//error is detected 
    here :java: no suitable method found for addAll(java.util.List<java.lang.String>)

}

}

带有搜索列表的我的 Hello 控制器

 @FXML
private Button btnhome;

@FXML
private Button btnsearch;

@FXML
private Button btnview;

@FXML
private ImageView image;

@FXML
private TextField numfeild;


@FXML
public TextField timefeild;

private static String lope;



public static ArrayList<String> blxckie = new ArrayList<String>();



@FXML
public <sel> File singleFileChooser(ActionEvent event) throws IOException {

    FileChooser fc = new FileChooser();
    File selectedFile = fc.showOpenDialog(null);

    if (selectedFile != null) {

        File sel = new File(selectedFile.getAbsolutePath());
        String lope = String.valueOf(sel);

        int countofwords = wordcount(sel);
        System.out.println("words are : " + countofwords);
        //numfeild.setText(Integer.toString(().throwDice()));

        getRandomWord(String.valueOf(sel));

        numfeild.setText(numfeild.getText() + countofwords);

        numfeild.setStyle("-fx-text-inner-color: blue");// to change text-field color


        return sel;

    } else {

        System.out.println("hmmm, atleast it works but still invalid file");

    }

    return null;


}


public int wordcount(File sel) {


    long start = System.currentTimeMillis();

    int countofwords = 0;

    try {


        BufferedReader br = new BufferedReader(new FileReader(sel));

        String lineStr = null;
        String wordsArr[] = null;

        while ((lineStr = br.readLine()) != null) {
            wordsArr = lineStr.split(" ");
            countofwords = countofwords + wordsArr.length;
        }
        br.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    long end = System.currentTimeMillis();
    long time = end - start;
    System.out.println(time);
    timefeild.setText(timefeild.getText() + time);
    timefeild.setStyle("-fx-text-inner-color: blue");// to change text-field color


    return countofwords;


}

public  String getRandomWord(String sel ) throws IOException {

    try (BufferedReader reader = new BufferedReader(new FileReader(sel))) {
        String line;
        while ((line = reader.readLine()) != null) {
            String[] wordline = line.split("\s+");

            if (blxckie.size() < 20) {
                for (String word : wordline) {

                    blxckie.add(word);
                    System.out.println(blxckie);
                }

            }






        }
        Random rand = new Random();
        return blxckie.get(rand.nextInt(blxckie.size()));
    }


}



@FXML
void searchpg(ActionEvent event) throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("search.fxml"));
    Parent root  = loader.load();

    SearchController searchControllera = loader.getController();
    


    Stage stage = new Stage();
    stage.setScene(new Scene(root));
    stage.show();



}



public List<String> searchList(String searchWords, List<String> listOfStrings)  {//searchlist
    List<String> searchWordsArray = Arrays.asList(searchWords.trim().split(""));

    return listOfStrings.stream().filter(input ->{
        return searchWordsArray.stream().allMatch(word ->
                input.toLowerCase().contains(word.toLowerCase()));
    }).collect(Collectors.toList());




}

我们将不胜感激任何形式的帮助

JavaFx 使用可观察对象,因此您不能将 List 设置为 ListView 的项目。 但是你可以用 FXCollections.observableArrayList(list) 包裹你的 List。 正如 khelwood 评论的那样,您必须将您的视图的泛型声明为 ListView<String>.

请注意,您必须 add/remove 元素 to/from ObservableList 才能看到 UI 中的修改。

如果你可以在控制器的 searchList 中更改 return 类型你也可以使用另一个 Collector 直接获得 ObersvableList: Collectors.toCollection(FXCollections::observableArrayList)