Francesco Galgani
Francesco Galgani

Reputation: 6249

Codename One - addActionListener of a Picker

The following code, in a lot of version of Android and iOS, shows the options "Cancel" and "OK". I would like that the addActionListener code is executed only when the user taps on the "OK" button. At the moment, that code is executed even if the user taps the "Cancel" button:

    Form hi = new Form("Hi World", BoxLayout.y());
    Picker languagePicker = new Picker();
    languagePicker.setType(Display.PICKER_TYPE_STRINGS);
    languagePicker.setStrings("Italian", "English", "German");
    languagePicker.setSelectedString("English");
    languagePicker.addActionListener((ev) -> {
        String newLanguage = languagePicker.getSelectedString();
        if (newLanguage != null && newLanguage.length() > 0) {
            Log.p("Language selected: " + newLanguage);
        }
    });
    hi.add(languagePicker);
    hi.show();

Upvotes: 1

Views: 180

Answers (2)

rainer
rainer

Reputation: 3411

I use the picker on several Android devices and they work as expected, meaning the action is performed only when the OK-Button is pressed.

Pressing the Cancel-Button does not incur in any action.

I could reproduce the error in the simulator, though.

Have you tried it on a device?

Upvotes: 1

Shai Almog
Shai Almog

Reputation: 52760

The Picker is a native component. As a result we just don't have that level of access or consistency when working with it. Keep in mind that in some forms it won't even have a list of elements so our control is very limited.

Upvotes: 1

Related Questions