Search code examples
comboboxcodenameonepicker

codenameone Picker Alternative to ComboBox


I am getting my feet wet with Codename One. I have looked into more other options like Xamarin, PhoneGap, Ionic for cross platform but I kinda got hooked with Codename one as it really code once and run anywhere.

I've been going through ui elements and I am kinda blocked on populating a combobox (Alternative is Picker)

Let's say I have stores as value pair (storeId, storeName). I want to display the storeName in Picker but keep storeId as the value reference. Once the store is selected I would like to pass the storeId to an API call.
Is this possible. This might be very simple question but seems bit difficult to implement (I am really new to mobile).

Thank you.


Solution

  • Our recommendation is to avoid ComboBox. It's a UI pattern that doesn't exist on iOS natively and would feel alien on modern phones. It exists in Codename One.

    In this code from the sample above you can get a similar effect to a complex multi-field combo box:

    Form hi = new Form("Button", BoxLayout.y());
    
    String[] characters = { "Tyrion Lannister", "Jaime Lannister", "Cersei Lannister"};
    String[] actors = { "Peter Dinklage", "Nikolaj Coster-Waldau", "Lena Headey"};
    int size = Display.getInstance().convertToPixels(7);
    EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(size, size, 0xffcccccc), true);
    Image[] pictures = {
        URLImage.createToStorage(placeholder, "tyrion","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/tyrion-lannister-512x512.jpg"),
        URLImage.createToStorage(placeholder, "jaime","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/jamie-lannister-512x512.jpg"),
        URLImage.createToStorage(placeholder, "cersei","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/cersei-lannister-512x512.jpg")
    };
    
    MultiButton b = new MultiButton("Pick A Lanister...");
    b.addActionListener(e -> {
        Dialog d = new Dialog();
        d.setLayout(BoxLayout.y());
        d.getContentPane().setScrollableY(true);
        for(int iter = 0 ; iter < characters.length ; iter++) {
            MultiButton mb = new MultiButton(characters[iter]);
            mb.setTextLine2(actors[iter]);
            mb.setIcon(pictures[iter]);
            d.add(mb);
            mb.addActionListener(ee -> {
                b.setTextLine1(mb.getTextLine1());
                b.setTextLine2(mb.getTextLine2());
                b.setIcon(mb.getIcon());
                d.dispose();
                b.revalidate();
            });
        }
        d.showPopupDialog(b);
    });
    hi.add(b);
    hi.show();
    

    enter image description here

    If you insist on using a ComboBox you can use a model to give it any object data you want. Then create a cell render to display the data. This is all discussed in depth in the component section of Codname One's developer guide. Notice that since ComboBox derives from List a lot of the List tips and docs apply to ComboBox.