MultipleSelectionModel 方法的 getSelectedItems 返回 Null

MultipleSelectionModel Method's getSelectedItems Returning Null

我在测试JavaFx 8 ListView的时候遇到了一个奇怪的问题。这是代码:

Main.java:

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Test.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        launch(args);
    }

}

TestController.java:

package application;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;

public class TestController implements Initializable 
{
    @FXML
    ListView<String> listView;

    private ArrayList <String> list;

    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        fillList();
        listView.getSelectionModel().getSelectedItems().addListener(
                (Change<? extends String> change) ->
                {
                    list.clear();

                    ObservableList<String> oList = listView.getSelectionModel().getSelectedItems(); 
                    System.out.println(oList);          
                });
    }

    private void fillList() 
    {
        list = new  ArrayList<>();
        list.add("1.item");
        list.add("2.item");
        list.add("3.item");

        ObservableList<String> items = FXCollections.observableArrayList(list);
        listView.setItems(items);

    }

}

Test.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane id="AnchorPane" prefHeight="399.0" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TestController">
   <children>
      <ListView fx:id="listView" layoutX="14.0" layoutY="85.0" prefHeight="200.0" prefWidth="200.0" />
   </children>
</AnchorPane>

当我先选择1.item,然后选择2.item,然后取消选择1.item(使用ctrl); listView.getSelectionModel().getSelectedItems() return null,程序输出为:

[1.item]
[1.item, 2.item]
[null]

但是当我先选择 2.item,然后选择 1.item,然后取消选择 2.item(使用 ctrl);结果正常,程序输出为:

[2.item]
[1.item, 2.item]
[1.item] 

有什么问题?

通过我的问题中的问题是由一个错误引起的,我分享了我针对这种情况的技巧(我在 getSelectedItems() return null 时使用了 getSelectedItem() 方法)。已更改 TestController.java:

package application;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;


public class TestController implements Initializable 
{

    @FXML
    ListView<String> listView;

    private ArrayList <String> list;



    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        fillList();
        listView.getSelectionModel().getSelectedItems().addListener(
                (Change<? extends String> change) ->
                {
                    list.clear();

                    ObservableList<String> oList = listView.getSelectionModel().getSelectedItems(); 

                    if(oList != null)
                        System.out.println(oList);
                    else
                        System.out.println(listView.getSelectionModel().getSelectedItem());
                });
    }

    private void fillList() 
    {
        list = new  ArrayList<>();
        list.add("1.item");
        list.add("2.item");
        list.add("3.item");

        ObservableList<String> items = FXCollections.observableArrayList(list);
        listView.setItems(items);

    }

}