How do I programatically set the selected item in a list or combobox

598 views
Skip to first unread message

steve...@more4apps.com

unread,
Dec 5, 2013, 9:25:21 PM12/5/13
to codenameone...@googlegroups.com
I want to programatically set the selected item in a list (a combobox actually but I think they essentially behave the same). I know the displayed value I need to find, how do I use this to search for the entry in the listmodel? I see the method setSelectedIndex does what I need but how do I determine what the index number is? I only have the text that is being displayed in the list.

Thanks,

Steve

Shai Almog

unread,
Dec 6, 2013, 2:38:06 AM12/6/13
to codenameone...@googlegroups.com, steve...@more4apps.com
There is also a setSelectedItem in List assuming you are using List and not the list model.

steve...@more4apps.com

unread,
Dec 8, 2013, 4:06:59 PM12/8/13
to codenameone...@googlegroups.com, steve...@more4apps.com

Hi Shai,
setSelectedItem takes "Object" as a parameter type, what would that object be? The comboBox is populated with a ListModel, how would I use setSelectedItem in this case?

Thanks,

Steve

Gareth Murfin

unread,
Dec 8, 2013, 4:37:55 PM12/8/13
to codenameone...@googlegroups.com, steve...@more4apps.com

steve...@more4apps.com

unread,
Dec 8, 2013, 4:40:45 PM12/8/13
to codenameone...@googlegroups.com, steve...@more4apps.com
Hi Gareth,
Do you have a code example for this, I am completely lost as to how I would "find out its index".

Thanks,

Steve

Gareth Murfin

unread,
Dec 8, 2013, 9:18:33 PM12/8/13
to codenameone...@googlegroups.com
Here is how I have done it in the past, it isnt lightning quick as it relies on string comparisons, but then it really doesnt matter if youre just setting a few items 


 cb = ((com.sun.lwuit.ComboBox  ) findByNameX("insurance_companycb", frm ));
            int index = getIndexOfThisString(cb,RegistrationInfoContainer.insurance_company,"updatingProfile a");
            setSelectedIndexOfComboBox(cb,index);




    //pass in a string and a comboxbox and get back the index of that string within the combobox
    private int getIndexOfThisString(ComboBox cb, String myString,String whoCalled)
    {
        _("getIndexOfThisString called by "+whoCalled);
        ListModel lm = cb.getModel();
        for (int i=0; i<lm.getSize(); i++)
        {
            String s = lm.getItemAt(i).toString();
            if (s.equals(myString))
            {
                return i;
            }
        }
        showError("getIndexOfThisString (for cb "+cb.getName()+") has failed to find the index of "+myString);
        return -1;
    }



  private void setSelectedIndexOfComboBox(ComboBox cb, int index)
    {
        _("setSelectedIndexOfComboBox "+cb.getName());
        if (index>=0)
        {
            cb.setSelectedIndex(index);
        }
        else
        {
            _("WARNING.................... setSelectedIndexOfComboBox got -1 so cant set index!!!");
        }
    }










--
You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/gqGH2PqRMtQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/7467c0d7-1f8f-481e-a31a-1120149a6058%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Gareth Murfin
(Android Freelancer - www.garethmurfin.co.uk)

Shai Almog

unread,
Dec 9, 2013, 1:24:08 AM12/9/13
to codenameone...@googlegroups.com, steve...@more4apps.com
The object would be the value you have, you said you have the string value of the entry you want to select right?
Reply all
Reply to author
Forward
0 new messages