• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to remove ArrayList<SelectItem> element

 
sa sam
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic