How to show the list data in search popup result using AutoCompleteTextField

66 views
Skip to first unread message

kousik mondal

unread,
Sep 28, 2016, 6:06:05 PM9/28/16
to CodenameOne Discussions
Hi ,


I want to search user data (using AutoCompleteTextField  ) based on name and also want to show the username with designation in two separate row in search list .
Like GenericListCellRenderer example in codename one I want to show the list in search popup but on selection it will select/show only user name in TextField.
But search has restriction of string array data. How can I implement this using AutoCompleteTextField ?

autocomplete-popup-list.png

Shai Almog

unread,
Sep 29, 2016, 3:02:16 AM9/29/16
to CodenameOne Discussions
Hi,
we restrict it to String since that is what a user types. You can set any renderer you want on the auto complete and store the meta data elsewhere.

kousik mondal

unread,
Oct 25, 2016, 3:01:21 AM10/25/16
to CodenameOne Discussions

My implementation :
================

public void search(){
   
    Form hi = new Form("Auto Complete", new BoxLayout(BoxLayout.Y_AXIS));
    // List list = new com.codename1.ui.List(createGenericListCellRendererModelData());
   
   
   
    Vector v = new Vector();
    for( int i =0; i<10 ; i++){
   
    Hashtable h = new Hashtable();
        h.put("name","Short-"+i);            
                 h.put("add","Kolkata-70005"+i);
                 
                 v.addElement(h);
    }
   
    //AutoCompleteTextField ac = new AutoCompleteTextField("Short", "Shock", "Sholder", "Shrek");
    final DefaultListModel options = new DefaultListModel(v);
    //  MyAutoCompleteTextField ac = new MyAutoCompleteTextField(options) ;
    AutoCompleteTextField ac = new AutoCompleteTextField(options);
     
    //======================
            ac.setMinimumElementsShownInPopup(5);
            ac.setCompletionRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
            hi.add(ac);
            
            hi.show();
   
    }


  private Container createGenericRendererContainer() {
       Label name = new Label();
       name.setFocusable(true);
       name.setName("name");
       Label add = new Label();
       add.setFocusable(true);
       add.setName("add");
       
      /* CheckBox selected = new CheckBox();
       selected.setName("Selected");
       selected.setFocusable(true);*/
       
    //   System.out.println("in renderer"+name.getText().toString());
       
       Container c = BorderLayout.center(name).
              add(BorderLayout.SOUTH, add);
             //  add(BorderLayout.WEST, selected);*/
       c.setUIID("ListRenderer");
       return c;
    }




The output:

After selection it's Throwing the following error:

java.lang.ClassCastException: java.util.Hashtable cannot be cast to java.lang.String
at com.codename1.ui.AutoCompleteTextField$4.actionPerformed(AutoCompleteTextField.java:324)
at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:459)
at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:362)
at com.codename1.ui.List.fireActionEvent(List.java:1563)
at com.codename1.ui.List.pointerReleasedImpl(List.java:2046)
at com.codename1.ui.List.pointerReleased(List.java:2056)
at com.codename1.ui.Form.pointerReleased(Form.java:2623)
at com.codename1.ui.Component.pointerReleased(Component.java:3226)
at com.codename1.ui.Display.handleEvent(Display.java:2022)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1067)
at com.codename1.ui.Display.mainEDTLoop(Display.java:996)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)


## the addPopup() method also private so I can not modify it.
## Filter will be based on name.



 

Shai Almog

unread,
Oct 25, 2016, 9:42:46 PM10/25/16
to CodenameOne Discussions
The keyword was "store the meta-data elsewhere". Auto-complete works only with Strings as it is based on a text field.

You can have a separate vector/map that includes the full additional meta data but naturally you can't add it to the list.
Reply all
Reply to author
Forward
0 new messages