Java-K
Java-K

Reputation: 497

FilterProxyListModel examples in CodenameOne

Since Codename One supports a subset of base Java Object Functionality, I can't use Vector.sort() to sort my data before adding it into a DefaultListModel. so Looking for other sorting options in Codename One, I found the FilterProxyListModel.

I can't find an example of how to properly initialize the FilterProxyListModel as it's using an interesting but tricky "Proxy" design pattern that I'm unfamiliar with. Here's how I've implemented it so far, but the component doesn't show any elements in the simulator when I do it this way. This is in the "initListModel..." method for my List from the GUI Builder:

protected boolean initListModelLearnableTopicsList(List cmp){

    Vector learnableListModel = new Vector;
    //omitting initialization of learnableListModel as a Vector of HashTables with key/value pairs to display
    ...

    FilterProxyListModel<DefaultListModel> fpListModel = new FilterProxyListModel<DefaultListModel>(new DefaultListModel(learnableListModel)) {
        @Override
        protected int compare(Object a, Object b, boolean ascending) {
            //details omitted... uses data in the LearnableListModel to provide sort order
        }

        @Override
        protected boolean check(Object o, String str) {
            //force all results to pass filter since original method fails when a Map object without a key of "name" is in the list
            return true;
        }

    };

    fpListModel.sort(true);

    cmp.setModel(fpListModel);
}

Am I doing something wrong here? is there an example somewhere I should be using as a guide to doing this properly?

Upvotes: 1

Views: 45

Answers (1)

Shai Almog
Shai Almog

Reputation: 52770

You can use Collections.sort(vector); the filter model is for list classes which we don't really recommend as much https://www.codenameone.com/blog/avoiding-lists.html

Upvotes: 0

Related Questions