Java没有为addListener找到合适的方法

时间:2018-11-28 15:51:31

标签: java listview javafx listener

我试图为列表视图生成一个侦听器,该侦听器获取存储在所选项目中的字符串,为其获取一个整数,然后调用一个方法。但是我一直得到

Error:(37, 67) java: no suitable method found for addListener(<anonymous javafx.beans.value.ChangeListener<java.lang.String>>)
    method javafx.beans.Observable.addListener(javafx.beans.InvalidationListener) is not applicable
      (argument mismatch; <anonymous javafx.beans.value.ChangeListener<java.lang.String>> cannot be converted to javafx.beans.InvalidationListener)
    method javafx.collections.ObservableList.addListener(javafx.collections.ListChangeListener<? super java.lang.String>) is not applicable
      (argument mismatch; <anonymous javafx.beans.value.ChangeListener<java.lang.String>> cannot be converted to javafx.collections.ListChangeListener<? super java.lang.String>)

错误。我有一个类似的侦听器,但是它只从列表中读取对象,而不是文本。

我的听众如下:

lstRequests.getSelectionModel().getSelectedItems().addListener(new ChangeListener<String>(){
                        @Override
                        public void changed(ObservableValue<? extends String> observable,
                                            String oldValue, String newValue) {
                                int requestID;
                                int endIndex = 5;
                                String requestIDSubString =
                                        newValue.substring(5,endIndex);
                                boolean isADigit;
                                do{
                                        isADigit =
                                                isDigit(requestIDSubString.charAt(endIndex));
                                        endIndex++;
                                        requestIDSubString =
                                                newValue.substring(5,endIndex);
                                } while (isADigit);
                                endIndex--;
                                requestID =
                                        Integer.parseInt(newValue.substring(5
                                                ,endIndex));
                                Request selectedRequest = null;
                                Boolean requestFound = false;
                                int checkIndex = 0;
                                do {
                                        Request checkRequest =
                                                requests.get(checkIndex);
                                        if (checkRequest.getRequestID() == requestID){
                                                selectedRequest = checkRequest;
                                                requestFound = true;
                                        }
                                } while (!requestFound);

                                populateItems(selectedRequest); //Calls the populate
                                // items method.
                        }
                });

我的进口是:

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import java.util.ArrayList;
import static java.lang.Character.isDigit;

有什么办法解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

当前,您正在尝试将ChangeListener添加到ObservableListgetSelectedItems()返回包含所有选定项目的ObservableListObservableList不允许您添加ChangeListener

如果要对单个选择使用侦听器,则应将侦听器添加到selectedItem属性:

lstRequests.getSelectionModel().selectedItemProperty().addListener(...);