File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes How to remove ArrayList<SelectItem> element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "How to remove ArrayList<SelectItem> element" Watch "How to remove ArrayList<SelectItem> element" New topic
Author

How to remove ArrayList<SelectItem> element

sa sam
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
i have ArrayList<SelectItem> class and i added items using



my problem is how can i remove item by name i.e. if i want to remove item from ArrayList<SelectItem> class whose name/label is "classA"
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

You have to use brute force, enumerate the SelectItem list until you find a match, then do a list.remove() operation on that SelectItem. It's not something that's common enough to mandate an API method of its own and it's not something complicated enough to be worth inflating the core JSF library over. I write a generic search/remove method for the one webapp I've got that does that kind of stuff and use it for al the SelectItem lists.

As long as the list is less than about 20 items, fancier search techniques don't actually help that much. And, as a general rule, you want to keep dropdown lists reasonably short or the users may decide to come and kill you.


Customer surveys are for companies who didn't pay proper attention to begin with.
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Sorry to resurrect an old thread...

If you have a SelectManyListbox, and you want to remove one or multiple selectItems, another option is to use a nested for loop.

As you know a SelectManyListbox uses a List for the SelectItems and possibly a String array (or collection or map) for the values in the listbox.

The outer for loop would be the length of the selected items in the list box, the inner for loop would be the size of the items in the listbox. Match the inner loop value to the outer loop value and remove the inner loop value at the inner loop index. (clear as mud, right?).


Thanks, leo
 
GeeCON Prague 2014
 
subject: How to remove ArrayList<SelectItem> element