我尝试使用此代码来获取一个选定的项目
public class sample implements Initializable {
@FXML
private ListView<String> list;
public void Select(){
ObservableList<String> selected =list.getSelectionModel().getSelectedItems();
for (int i =0; i<selected.size();i++)
System.out.println(selected.get(i));
}
@Override
public void initialize(URL location, ResourceBundle resources) {
list.getItems().addAll(Arrays.asList("item1", "item2", "item3"));
list.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
}
但这返回了一个元素,我需要选择多个元素。输出如下所示: