diff --git a/src/com/multiselectspinner/widget/MultiSelectSpinner.java b/src/com/multiselectspinner/widget/MultiSelectSpinner.java index 37f1356..d7ff37b 100644 --- a/src/com/multiselectspinner/widget/MultiSelectSpinner.java +++ b/src/com/multiselectspinner/widget/MultiSelectSpinner.java @@ -35,6 +35,7 @@ */ public class MultiSelectSpinner extends Spinner implements OnMultiChoiceClickListener { String[] _items = null; + Long[] _ids = null; boolean[] _selection = null; ArrayAdapter _proxyAdapter; @@ -121,6 +122,22 @@ public void setItems(List items) { Arrays.fill(_selection, false); } + /** + * Sets the id foreach item in the spinner. + * @param ids + */ + public void settIds(Long[] ids){ + _ids = ids; + } + + /** + * Sets the id foreach item in the spinner. + * @param ids + */ + public void setIds(List ids) { + _ids = ids.toArray(new Long[ids.size()]); + } + /** * Sets the selected options based on an array of string. * @param selection @@ -192,6 +209,25 @@ public List getSelectedIndicies() { return selection; } + /** + * Returns a list of ids, one for each selected item. + * + * @return + */ + public List getSelectedIds() { + if(_ids == null) + throw new IllegalStateException("This method should only be called if setIds is called"); + if(_ids.length != _items.length) + throw new IllegalStateException("The number of ids should match the number of items"); + List selection = new LinkedList(); + for (int i = 0; i < _items.length; ++i) { + if (_selection[i]) { + selection.add(_ids[i]); + } + } + return selection; + } + /** * Builds the string for display in the spinner. * @return comma-separated list of selected items