简体   繁体   中英

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

I want to pass an array list. I think the error is caused by the search list. i know The addAll method works with collections. Thats why its saying my searchlist can't be converted to a collection, how can i solve this This is the error

my seacrh controller

@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>)

}

}

My Hello controller with searchlist

 @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());




}

Anyform of help will be muchly appriciated

JavaFx uses observable objects so you cannot set a List as items for your ListView . But you can wrap your List with FXCollections.observableArrayList(list) . And as khelwood commented, you have to declare the generics of your view as ListView<String> .

Note that you have to add/remove the elements to/from the ObservableList to see the modification in your UI.

If you can change the return type in the controller's searchList you can also use another Collector to directly get the ObersvableList : Collectors.toCollection(FXCollections::observableArrayList)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM