codenameone / CodenameOne

Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.
https://www.codenameone.com/
Other
1.71k stars 405 forks source link

It's too hard/impossible to create a truly dynamic AutoCompleteTextField #1694

Closed codenameone closed 8 years ago

codenameone commented 8 years ago

With the webservice defined here:

    java.util.List<Map<String, Object>> searchLocations(String text) {        
        try {
            ConnectionRequest r = new ConnectionRequest();
            r.setPost(false);
            r.setUrl("http://api.nestoria.co.uk/api");
            r.addArgument("pretty", "0");
            r.addArgument("action", "search_listings");
            r.addArgument("encoding", "json");
            r.addArgument("listing_type", "buy");
            r.addArgument("page", "1");
            r.addArgument("country", "uk");
            r.addArgument("place_name", text);        
            NetworkManager.getInstance().addToQueueAndWait(r);
            Map<String,Object> result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(r.getResponseData()), "UTF-8"));
            Map<String, Object> response = (Map<String, Object>)result.get("response");
            List<Map<String, Object>> results = (List<Map<String, Object>>)response.get("locations");
            return results;
        } catch(Exception err) {
            Log.e(err);
            return null;
        }
    }

I would expect this AutoCompleteTextField to work but it doesn't:

        Form hi = new Form("Auto Complete", new BoxLayout(BoxLayout.Y_AXIS));
        final DefaultListModel<String> options = new DefaultListModel<>();
        AutoCompleteTextField ac = new AutoCompleteTextField(options) {
            @Override
            protected boolean filter(String text) {
                if(text.length() == 0) {
                    return false;
                }
                java.util.List<Map<String, Object>> l = searchLocations(text);
                if(l == null || l.size() == 0) {
                    return false;
                }

                options.removeAll();
                for(Map<String, Object> o : l) {
                    options.addItem((String)o.get("place_name"));
                }
                return true;
            }

        };
        ac.setMinimumElementsShownInPopup(5);
        hi.add(ac);

I think we need to rewrite the component to provide an option that doesn't use a list (e.g. for more customizability) and to provide better dynamic fill.

chen-fishbein commented 8 years ago

Fixed this