设备在codenameone中的下拉错误

时间:2018-08-08 14:21:29

标签: codenameone

我在我的应用程序中实现了溺水功能。它使用带有元素列表的容器来实现。 假设下拉列表具有以下项aa1,aa2,aa3,aa4,aa5等。如果我搜索为“ aa”,则显示从“ aa”开始的项目,如果我从列表中选择aa5,则显示aa1并将其显示。但是,如果我滚动项目并选择其工作正常。仅在iOS设备上在模拟器上正常工作时会出现此问题。 第一张图片描绘了下拉菜单的样子,第二张图片如果我搜索“ ee”,它给出了以“ ee”开头的项目列表。如果选择“ ee5”,则将其设置为ee1,如图3所示。仅在设备上有问题。有任何解决方法吗?

所以,请让我知道这是什么问题。

谢谢

[![在此处输入图片描述] [1]] [1] enter image description here enter 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);
                    }

                }

            };

    }
}

//使用多按钮实现的新代码

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();
        });

1 个答案:

答案 0 :(得分:1)

我建议使用Container和简单的布局搜索,如此类代码所示。以下代码摘自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();