第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將 Property<Predicate<MyObject>> 綁定到 ListView

將 Property<Predicate<MyObject>> 綁定到 ListView

溫溫醬 2021-07-09 14:47:41
我試圖在一個(gè)ListView可以選擇多個(gè)項(xiàng)目的地方和一個(gè)ObjectProperty. 當(dāng)一個(gè)項(xiàng)目被選中時(shí),我想相應(yīng)地過濾一TableView列:使用兩個(gè)較低的過濾器(組件、詳細(xì)信息),我進(jìn)行如下綁定:ObjectProperty<Predicate<Log>> detailsSearchFilter = new SimpleObjectProperty<>();TextField detailsSearchField = new TextField();detailsSearchField.setPromptText("e.g. finished initializing");detailsSearchFilter.bind(Bindings.createObjectBinding(() ->                    log -> log.getDetails().toLowerCase().contains(detailsSearchField.getText().toLowerCase()),            detailsSearchField.textProperty()    ));然后稍后添加邏輯運(yùn)算符方法and()以便能夠組合所有過濾器:logFilteredList.predicateProperty().bind(Bindings.createObjectBinding(() ->                    detailsSearchFilter.get().and(componentSearchFilter.get()).and(sourceFilter.get()),            detailsSearchFilter, componentSearchFilter, sourceFilter    ));對(duì)于其他兩個(gè)ListView過濾器,我正在考慮做這樣的事情:private final static String[] sources = new String[]{"ECS","IISNode","PrismWebServer"};ListView<String> sourceList = new ListView<>();ObservableList sourceItems = FXCollections.observableArrayList(sources);sourceList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);sourceList.getItems().addAll(sources);ListView<String> selected = new ListView<>();sourceList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {        selected.setItems(sourceList.getSelectionModel().getSelectedItems());        System.out.println(Arrays.toString(selected.getItems().toArray()));    });因此該selected列表現(xiàn)在包括當(dāng)前選擇的所有值。如何將sourceFilter與selected列表的所有值綁定?我正在考慮遍歷列表中的每個(gè)值并以這種方式綁定:  sourceFilter.bind(Bindings.createObjectBinding(() ->            log -> log.getSource().toLowerCase().contains(selected.getItems().get(i).toLowerCase()),                sourceList.selectionModelProperty()    ));但這似乎不是很優(yōu)雅,我不確定我是否selectionModelProperty以正確的方式使用。
查看完整描述

1 回答

?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

的selectionModelProperty時(shí)候才會(huì)觸發(fā)改變selectionModel被替換這通常不會(huì)發(fā)生。依賴關(guān)系應(yīng)該是sourceList.getSelectionModel().getSelectedItems().


此外,按照我的理解,您應(yīng)該查詢Log'ssource是否在所選項(xiàng)目中,而不是該source字符串是否在所選項(xiàng)目列表中的某個(gè)位置包含字符串的某些部分。


另請(qǐng)注意,ListView應(yīng)避免僅使用 a來存儲(chǔ)數(shù)據(jù),對(duì)于大型列表,contains對(duì) aSet而不是 a進(jìn)行檢查效率更高List。


你可以使用這樣的代碼:


ObjectBinding<Predicate<Log>> binding = new ObjectBinding<Predicate<String>>() {

    private final Set<String> strings = new HashSet<>();


    {

        sourceList.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<String>() {


            @Override

            public void onChanged(ListChangeListener.Change<? extends String> c) {

                boolean changed = false;


                // modify set on selection change

                while (c.next()) {

                    if (c.wasRemoved()) {

                        changed = true;

                        c.getRemoved().stream().map(String::toLowerCase).forEach(strings::remove);

                    }

                    if (c.wasAdded()) {

                        changed = true;

                        c.getAddedSubList().stream().map(String::toLowerCase).forEach(strings::add);

                    }

                }


                if (changed) {

                    invalidate();

                }

            }

        });

    }


    @Override

    protected Predicate<Log> computeValue() {

        return log -> strings.contains(log.getSource().toLowerCase());

    }


};

sourceFilter.bind(binding);


查看完整回答
反對(duì) 回復(fù) 2021-07-14
  • 1 回答
  • 0 關(guān)注
  • 310 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)