2
votes

My app uses a Codename One (lightweight) picker for cases where a user should choose one item from a list. Sample code:

Picker pick = new Picker();
pick.setType(Display.PICKER_TYPE_STRINGS);
pick.setUseLightweightPopup(true); // using localization if needed
pick.setStrings(new String[] { "1", "2", "3", "4" });
pick.setSelectedStringIndex(0);

Now I am surprised that it is possible for the user to select a null value, i.e., a value that is not in the list. This happens when the user scrolls until no value is visible. Sometimes the picker scrolls back to the last value automatically, sometimes it doesn't.

enter image description here

If no value is visible and the user taps Done, a null value is assigned to the picker and it shows the text '...'. In my opinion this is incorrect behaviour: it should not be possible to select anything other than values from the list. My code depends on that.

For now I can use the workaround code below but the possibility of selecting a null value is not pretty.

// workaround
pick.addActionListener((ActionListener) (ActionEvent evt) -> {
    if (pick.getSelectedString() == null) {                        // if no value
        pick.setSelectedStringIndex(pick.getStrings().length - 1); //    set to last value
    }
});

Is this something that needs to be fixed?

Edit: tested in the simulator and on iPhone devices. On Android it works fine: it is not possible to scroll beyond the last item in the list.

This sounds like something that should go in the issue tracker since you have a test case and it reproduces in the simulator right? github.com/codenameone/CodenameOne/issuesShai Almog