我使用 Androids 绑定来填充我的 Spinner 并在模型中设置所选项目。到目前为止,这工作正常。但是,当我加载具有先前设置的 Spinner 值的模型时,它总是设置为 Spinners 列表中的第一项。
这是来自布局文件的 Spinners 节选:
<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)}"/>
以及用于绑定的转换方法:
@InverseMethod("toPhoneType")
fun toInt(@TYPE phoneType: String): Int {
return TYPES.indexOf(phoneType)
}
fun toPhoneType(@TYPE ordinal: Int): String {
return TYPES[ordinal]
}
我找到了这个旧帖子,但(已接受)解决方案已经适用于我的代码。