Assigning default values to spinners

When using spinners in android you might have the problem of setting it to a default value before you render it.

The way to go about is :-



ArrayAdapter<String> spinnerAdap = (ArrayAdapter<String>) mySpinner.getAdapter();
int spinnerPosition = messgAdap.getPosition(theDefaultValue);
mySpinner.setSelection(spinnerPosition);



Example: 

1.Your spinner has values from 1,2,3...10 as a String ArrayList. To set its default value to 3 you have to fetch the position of "3" as above.

2.You fetch the adapter from the spinner and get the position of the "default" value, "3" here, from the adapter.

2. Set the spinner to position of "3"

No comments:

Post a Comment