Search code examples
javafxwrappertreetable

How to setCellValueFactory return type for ArrayList


I have following error in this code: Cannot infer type arguments for ReadOnlyListWrapper<> How should my return type look like? I need to save arraylist for each node in all columns. But I can not return it.

        for (Entry<String, String> ent : dc.getSortedOrgAll().entrySet()) {

        TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>(
                ent.getValue());

        col.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<String, ArrayList<String>>, ObservableValue<ArrayList<String>>>() {
            @Override
            public ObservableValue<ArrayList<String>> call(
                    CellDataFeatures<String, ArrayList<String>> param) {
                TreeMap<String, List<String>> temp = (TreeMap<String, List<String>>) dc
                        .getFuncTypeOrg().clone();
                ArrayList<String> result =  new ArrayList<>();
                for (int i = 0; i < temp.size(); i++) {
                    List<String> list = temp.firstEntry().getValue();
                    String key = temp.firstEntry().getKey();
                    // root.getChildren();

                    if (list.get(1).equals("Papier")) {
                        System.out.println(list.get(1));
                    }
                    if (list.get(1).equals(param.getValue().getValue())
                            && list.get(5).equals(col.getText())) {
                        result.add(list.get(2));
                        if(list.size()==9)
                        result.add(list.get(list.size()-1));
                        else result.add("White");

                    } else {
                        temp.remove(key);
                    //  result = null;
                    }

                }

                return new ReadOnlyListWrapper<>(result);
            }
        });

Solution

  • ReadOnlyListWrapper<T> implements ObservableValue<ObservableList<T>>, which isn't what you need, as you declared the callback to return an ObservableValue<ArrayList<T>> (T is just String here).

    So I think you just need

    return new ReadOnlyObjectWrapper<ArrayList<String>>(result);
    

    and you can probably omit the generic type:

    return new ReadOnlyObjectWrapper<>(result);
    

    Just a comment: I think you could make your life much easier by defining some actual data model classes, instead of trying to force your data into various collections implementations.