我尝试为列表视图生成一个监听器,该监听器获取存储在所选项目中的字符串,为其获取一个整数,然后调用一个方法。然而,我一直得到
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;
有什么办法解决这个问题吗?
当前您正在尝试将ChangeListener
添加到ObservableList
。CCD_ 3返回包含所有所选项目的CCD_。ObservableList
不允许您添加ChangeListener
。
如果要对单个选择使用侦听器,则应将侦听器添加到selectedItem
属性:
lstRequests.getSelectionModel().selectedItemProperty().addListener(...);