I use Androids binding to populate my Spinner and set the selected item in the model. This works fine so far. But when I load a model with previously set Spinner value it gets always set to the first item in the Spinners list.
This is the Spinners excerpt from the layout file:
<Spinner
android:id="@+id/typeSpinner"
android:layout_width="match_parent"
android:layout_height="@dimen/spinner_height"
android:layout_marginEnd="16dp"
android:entries="@{obj.CREATOR.TYPES}"
android:selectedItemPosition="@={obj.toInt(obj.type)}"/>
And the conversion methods used for the binding:
@InverseMethod("toPhoneType")
fun toInt(@TYPE phoneType: String): Int {
return TYPES.indexOf(phoneType)
}
fun toPhoneType(@TYPE ordinal: Int): String {
return TYPES[ordinal]
}
I found this old post, but the (accepted) solution already applies to my code.