搜索过滤器中的代号为ONE



我正在尝试在包含一组按钮的容器中实现搜索过滤器。

这是我的代码:

public void listMenu() {
    Dialog loading = new InfiniteProgress().showInifiniteBlocking();
    loading.show();
    final Form listMenu = new Form("List Menu");
    listMenu.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    Container list = new Container();
    list.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    list.removeAll();
    Button back = new Button("Back to Main Menu");
    ParseQuery<ParseObject> query = ParseQuery.getQuery("mylist");
    query.whereExists("Title");
    List<ParseObject> results = null;
    try {
        Button btn = null;
        results = query.find();
        if(!results.isEmpty()) {
        int index = 0;
        int size = results.size();
        for(;index < size;++index) {
            list.add(btn = new Button(results.get(index).getString("Title")));
            addListener(btn);
        }
        }
    } catch (com.parse4cn1.ParseException e) {
        Dialog.show("Err", "Server is not responding.", "OK", null);
    }
    listMenu.add(list);
    listMenu.add(back);
    listMenu.show();
    loading.dispose();
    back.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent ev) 
        {
            new StateMachine("/theme");
        }
    });
}

此代码基本上是从数据库中查询数据,然后将其结果设置为按钮,然后将其添加到容器中。我的问题是如何将搜索过滤器实现到我的容器中?我已经看过FilterProxyListModel<T>,但不确定ListModel<T>是否与Container兼容。我很高兴能在我的代码中看到搜索过滤器实现的示例。

FilterProxyListModel适用于List,我们不再建议了。这里有一个完整的搜索容器的示例。它使用MultiButton,但使用Button也可以工作:

hi.getToolbar().addSearchCommand(e -> {
    String text = (String)e.getSource();
    if(text == null || text.length() == 0) {
        // clear search
        for(Component cmp : hi.getContentPane()) {
            cmp.setHidden(false);
            cmp.setVisible(true);
        }
        hi.getContentPane().animateLayout(150);
    } else {
        text = text.toLowerCase();
        for(Component cmp : hi.getContentPane()) {
            Button mb = (Button)cmp;
            String line1 = mb.getText();
            boolean show = line1 != null && line1.toLowerCase().indexOf(text) > -1;
            mb.setHidden(!show);
            mb.setVisible(show);
        }
        hi.getContentPane().animateLayout(150);
    }
}, 4);

最新更新