I'm building a dialog with a spinner. When the dialog is done, it calls a method of the parent activity with a string argument - the argument being the string value that was selected.
My current approach:
I'm setting up the spinner's array adapter like so:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,
categoryNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
categoryNames
is a string array. When the dialog is done, the selected categoryName
is used as the parameter to the method call on the parent activity.
What I really want to do:
What I actually want is to display a list of Category
objects. The Category
class has 2 properties - categoryId
and categoryName
. The spinner should still display the categoryName
s in the drop-down view, but when the dialog is done, it should be able to unambiguously tell which Category
was selected, and call the parent activity's callback method with the categoryId
of the category that was selected.
There can be multiple Category
ies with the same categoryName
.
Question: How to do the above?