priya
priya

Reputation: 468

dropdown error on device in codenameone

I am implementing drop-drown feature in my app. Its implemented using container with list of elements in it. Suppose the drop down list has following items aa1, aa2, aa3, aa4 aa5 and so on. And if i search as 'aa' it displays items starting from 'aa', if I select aa5 from list, it takes aa1 and displays that. But whereas if I scroll the items and select its working fine. This problem occurring only on iOS device working perfectly fine on simulator. the first picture depicts how drop down looks like, in second picture if I search 'ee', it gives list of items starting with 'ee'. If I select 'ee5', it sets to ee1 as shown in picture 3. Problem only on device. Any workaround for this?

So, please let me know whats the issue with this.

Thanks

[![enter image description here][1]][1]enter image description hereenter image description here

private CustomList itemList;

class CustomList extends List {
    int startYPos = -1;
    long lastDiff = 0;
    Timer t = null;
    int draggingState = 0; 

    public CustomList() {

        this.setTensileDragEnabled(false);

    }

}

private class ButtonListener implements ActionListener {

    public void actionPerformed(final ActionEvent evt) {


            final Runnable rn = new Runnable() {
                public void run() {
                    // Create and show a dialog to allow users to make a selection.
                    final UiBuilder uib = dm.UiBuilder();

                    dialog = (Dialog) uib.createContainer(DESIGNER_NAME_DIALOG_COMBOBOX_CONTAINER);
                    GenericSpinner itemSpinner = (GenericSpinner) uib.findByName(DESIGNER_NAME_DIALOG_COMBOBOX_GENERIC_SPINNER, dialog);
                    itemSpinner.setPreferredW(Display.getInstance().getDisplayWidth() * 4 / 5);

                    //remove from parent and replace with a linear list
                    Container parent = itemSpinner.getParent();
                    parent.removeComponent(itemSpinner);

                    // Add the searchable text field box
                    final TextField tf = (TextField)uib.findByName("Search", dialog);


                    tf.addDataChangedListener(new DataChangedListener() {

                        @Override
                        public void dataChanged(int type, int index) {
                            Object[] items = model.getFilteredItems(tf.getText());  
                            itemList.setModel(new DefaultListModel(items));

                        }
                    });


                    itemList = new CustomList();
                    itemList.getAllStyles().setBgTransparency(0);

                    itemList.setItemGap(0);

                    parent.addComponent(BorderLayout.CENTER, itemList);

                    final String[] items = model.getItems();

                    itemList.setModel(new DefaultListModel(items));

                    itemList.getStyle().setMargin(10,  10, 10, 10);

                    itemList.setFireOnClick(true);    


                    itemList.setLongPointerPressActionEnabled(false);


                    ActionListener list = new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            if (model.isUserEditable() && model.getItemCount() > 0) {
                                int i = itemList.getSelectedIndex();
                                if (i > items.length - 1) {
                                    return;
                                }

                                itemList.getModel().setSelectedIndex(i);
                                model.onUserDataEntered((String) itemList.getModel().getItemAt(i)); 
                                String textToDisplay = (String) itemList.getModel().getItemAt(i);


                                button.setText(textToDisplay);

                            }
                            dialog.dispose();

                        }

                    };


                    itemList.addActionListener(list);

                    CommonTransitions tran = CommonTransitions.createEmpty();
                    dialog.setTransitionInAnimator(tran);
                    dialog.setTransitionOutAnimator(tran);  
                    itemList.setRenderer(new ListRenderer());

                    //related to dialog to show list of items
                    //how much space do we really need???
                    if (cellHeight == 0) {
                        int dip = Display.getInstance().convertToPixels(1);
                        int siz = 2;
                        if (Display.getInstance().isTablet()) {
                            siz = 4;
                        }
                        siz *= 2;

                        cellHeight = siz * dip;
                    }
                    int heightRequired = cellHeight * (items.length + 8);
                    //is this too much for the screen - we will use 3/4 of the screen height max
                    int availableHeight =  Display.getInstance().getDisplayHeight() * 3;
                    availableHeight /= 4;
                    if (heightRequired > availableHeight) {
                        int topPos = Display.getInstance().getDisplayHeight() / 8;
                        int bottomPos = topPos +  availableHeight;
                        dialog.show(topPos, topPos, 40, 40);
                    }
                    else {
                        int topPos = (Display.getInstance().getDisplayHeight() - heightRequired) / 2;
                        int bottomPos = topPos + heightRequired;
                        dialog.show(topPos, topPos, 40, 40);
                    }

                }

            };

    }
}

//new code using Multibutton implementation

final String[] listItems = model.getItems();

        Display.getInstance().callSerially(() ->{

                multiButton= new MultiButton();
                multiButton.setTextLine1(s);

                dialog.add(multiButton);
                multiButton.addActionListener(e -> Log.p("you picked " + multiButton.getSelectCommandText(), Log.ERROR));

            }
            dialog.revalidate();
        });

Upvotes: 1

Views: 41

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

I would recommend using a Container and simple layout search as demonstrated by code such as this. The code below was taken from the Toolbar javadoc:

Image duke = null;
try {
    duke = Image.createImage("/duke.png");
} catch(IOException err) {
    Log.e(err);
}
int fiveMM = Display.getInstance().convertToPixels(5);
final Image finalDuke = duke.scaledWidth(fiveMM);
Toolbar.setGlobalToolbar(true);
Form hi = new Form("Search", BoxLayout.y());
hi.add(new InfiniteProgress());
Display.getInstance().scheduleBackgroundTask(()-> {
    // this will take a while...
    Contact[] cnts = Display.getInstance().getAllContacts(true, true, true, true, false, false);
    Display.getInstance().callSerially(() -> {
        hi.removeAll();
        for(Contact c : cnts) {
            MultiButton m = new MultiButton();
            m.setTextLine1(c.getDisplayName());
            m.setTextLine2(c.getPrimaryPhoneNumber());
            Image pic = c.getPhoto();
            if(pic != null) {
                m.setIcon(fill(pic, finalDuke.getWidth(), finalDuke.getHeight()));
            } else {
                m.setIcon(finalDuke);
            }
            hi.add(m);
        }
        hi.revalidate();
    });
});

hi.getToolbar().addSearchCommand(e -> {
    String text = (String)e.getSource();
    if(text == null || text.length() == 0) {
        // clear search
        for(Component cmp : hi.getContentPane()) {
            cmp.setHidden(false);
            cmp.setVisible(true);
        }
        hi.getContentPane().animateLayout(150);
    } else {
        text = text.toLowerCase();
        for(Component cmp : hi.getContentPane()) {
            MultiButton mb = (MultiButton)cmp;
            String line1 = mb.getTextLine1();
            String line2 = mb.getTextLine2();
            boolean show = line1 != null && line1.toLowerCase().indexOf(text) > -1 ||
                    line2 != null && line2.toLowerCase().indexOf(text) > -1;
            mb.setHidden(!show);
            mb.setVisible(show);
        }
        hi.getContentPane().animateLayout(150);
    }
}, 4);

hi.show();

Upvotes: 1

Related Questions