1
votes

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?

1
Update: I realized that part of the sort method excludes all entries that are Map objects without a "name" key with a string value. it's the check method. Since I'm only using sort and not filter functionality of the FilterProxyListModel, I overrode the check() method with return true; so that all entries pass the filter. That said, the question still stands: Where is there a reference example of how to use FilterProxyListModel? if there isn't one yet, there ought to be... - Java-K

1 Answers

0
votes

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