Filtered list is never shown, List doesn't change

48 views
Skip to first unread message

Greenkohl23

unread,
Mar 3, 2014, 3:38:42 AM3/3/14
to codenameone...@googlegroups.com
Hi,

I used a Filter on my List. I implemented a searchfield and onChange it filters the List. I Override the filter methode, because i have a hashtable with 2 arguments which should be compared to the filter argument. Filtering is working, but the list doesn't change. Where is my fault?

 final TextField search = (TextField) findByName("Search", f);
        search.addDataChangeListener(new DataChangedListener() {
            public void dataChanged(int type, int index) {
                Log.p("Searchfield changed", Log.DEBUG);

                String searchEntry = search.getText();
                Log.p("Search Entry: " + searchEntry);

                List list = (List) findByName("MultiList", search.getComponentForm());
                FilterProxyListModel f = new FilterProxyListModel(list.getModel()) {
                    private ListModel underlying = this.getUnderlying();
                    private ArrayList<Integer> filter;
                    
                    @Override
                    public void filter(String str) {
                        filter = new ArrayList<Integer>();
                        str = str.toUpperCase();
                        for (int iter = 0; iter < underlying.getSize(); iter++) {
                            Object o = underlying.getItemAt(iter);
                            if (o != null) {
                                Hashtable h = (Hashtable) o;
                                Object val = h.get("ssid");
                                Object val2 = h.get("strasse");
                                if ((val != null && ((String)val).toUpperCase().indexOf(str) > -1)) {
                                    filter.add(new Integer(iter));
                                } else if ((val2 != null && ((String)val2).toUpperCase().indexOf(str) > -1)) {
                                    filter.add(new Integer(iter));
                                }
                            }
                        }
                    }

                };
                f.filter(searchEntry);
                list.setModel(f);
                list.getParent().revalidate();


Shai Almog

unread,
Mar 3, 2014, 12:17:37 PM3/3/14
to codenameone...@googlegroups.com
Hi,
I suggest overriding this instead:
protected boolean check(Object o, String str)

Which will work seamlessly. You didn't fire the data change event.

Greenkohl23

unread,
Mar 4, 2014, 2:32:20 AM3/4/14
to codenameone...@googlegroups.com
Hi,

thx for your help.

I changed it and override the check method. That fixed my Problem, so the list is filtered. But, if my searchfield is empty it doesn't recognize to reset the filter. I think there is a fault in using the FilterProxyModel right.

I tried to use:

f.install(search, list); 

instead of:

f.filter(searchEntry); list.setModel(f);

But this ignores my override on: 
check(Object o, String str);

I think I am close to the solution, but I need another hint :D

Greenkohl23

unread,
Mar 4, 2014, 2:41:58 AM3/4/14
to codenameone...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages