androidBeckhamania androidBeckhamania - 1 year ago 67
Android Question

How to keep a value selected in android spinner based on database value?

I have populated an android spinner from database in one activity.
But on the edit part of activity I wanna keep the android spinner selected to particular value equivalent to current record.
When I use

spinner.setSelection(keepSelectedStateID);


It keeps it selected by position I wanna keep it selected by ID or value based from record in db.
Thanks In Advance.

Answer Source

you can use this method after poputlating spinner. pass spinner and value to this method.

public static void setSpinnerValue(Spinner spin, String value) {

    for (int i = 0; i < spin.getCount(); i++) {
        if (spin.getItemAtPosition(i).toString().equalsIgnoreCase(value)) {
            spin.setSelection(i);
            break;
        }

    }
}