dropdown error on device in codenameone

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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





How does that code look? These are components you wrote not builtin components so I'm assuming something along the way failed
– Shai Almog
Aug 9 at 4:13





@ShaiAlmog, I have added the code which implement this. If I set itemList.setFireOnClick(true); , it selects first item from list. But when its false, it selects the item after clicking two times. Please provide any workaround for this. Thanks
– priya
Aug 9 at 13:51





What's I'm looking for is the code of the filtering which is where you say the bug exists. It seems you are using List despite our recommendation to avoid it codenameone.com/blog/avoiding-lists.html
– Shai Almog
Aug 10 at 4:02


List





@ShaiAlmog Thanks for this workaround. I implemented using Multibutton now, but whenever I select item from drop down, it just returns me last entered value. The code I used for implementation is below.
– priya
Aug 13 at 11:41





It happens because you store all buttons to one global button instead of using a local reference of the new button.
– Shai Almog
Aug 14 at 4:12




1 Answer
1



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:


Container


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 -> 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
hi.getContentPane().animateLayout(150);

, 4);

hi.show();






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard