获取listview JAVAFX中多个所选项的索引

时间:2016-09-28 15:24:57

标签: javafx

有人可以帮助我获取列表视图中所选项目的索引。 我在JAVA FX尝试但我的所有工作都是徒劳。

2 个答案:

答案 0 :(得分:2)

使用ListView listView = ... 获取这些索引的列表:

ObservableList<Integer> selectedIndices = listView.getSelectionModel().getSelectedIndices();
    public class CarGallery extends ListFragment {

        public static final String ROW_ID = "row_id"; // Intent extra key
        private SimpleCursorAdapter myCarSimpleCursorAdapter;
        private Cursor mCursor;
        private String[] from;
        private int[] to;

        @Override
        public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
            setHasOptionsMenu(true);
            // map each car details to a TextViews in the ListView layout
            from = new String[]{"number", "manufacturer", "model", "img"};
            // in file  car_row.xml
            to = new int[]{R.id.textViewNumber, R.id.textViewManufacturer, R.id.textViewModel, R.id.imageViewCar};
            //Create Empty Adapter
            myCarSimpleCursorAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(),
                    R.layout.car_row, // layout to inflate it
                    mCursor, // cursor adapter : if null then it will be created within the constructor
                    from, to, 0);

    // set the Adapter on ViewBinder Class 
            myCarSimpleCursorAdapter.setViewBinder(new MyViewBinder());
            setListAdapter(myCarSimpleCursorAdapter);  //databinding --> set contactView's adapter
}

答案 1 :(得分:0)

这会将选定的行捕获到变量中,供您随意使用:

int selectedIndex = listView.getSelectionModel().getSelectedIndex();