James N
James N

Reputation: 511

Toolbar Search SwipableContainer Codename One

I am trying to use the toolbar search feature to search through a number of SwipeableContainers. Each container has a MultiButton on top and a number of buttons bottom left and bottom right. Essentially I receive data from a database and loop through the result adding a SwipeableContainer and set each one with a name (Line1 of the MultiButton) using sc.setName(). I then attempt to search using the code below:

Here is the code:

hi.getToolbar().addSearchCommand(e -> {
        String text = (String)e.getSource();
        if(text == null || text.length() == 0) {
            // clear search
            for(Component cmp : centercont) {
                cmp.setHidden(false);
                cmp.setVisible(true);
            }
            centercont.animateLayout(150);
        } else {
            text = text.toLowerCase();
            for(Component cmp : centercont) {                
                SwipeableContainer sc = (SwipeableContainer)cmp;
                String scName = sc.getName();
                boolean show = text.length() == 0 || scName.toLowerCase().contains(text);
                sc.setHidden(!show);
                sc.setVisible(show);
            }
            centercont.animateLayout(150);
        }
        }, 4);

After I input the first character into the search I get this exception: java.lang.ClassCastException: com.codename1.ui.Label cannot be cast to com.codename1.ui.SwipeableContainer. If I press 'OK' past the error dialog, the search filters the options as expected for that 1 character. I get the same exception and result for the next character and so on.

I would appreciate some guidance on where I have gone wrong.

Upvotes: 1

Views: 93

Answers (1)

Shai Almog
Shai Almog

Reputation: 52770

You have more than one component within centercont. One of them is a SwipeableContainer and the other is a Label.

You can workaround it by checking with instanceof before doing the cast but you might want to check your code/component inspector to see what's that label and if it should be there.

Upvotes: 1

Related Questions