我收到此错误:
但是我在Converters
上有这个方法:(我也尝试过在其中带有伴侣对象的类)
object Converters {
fun indexFromModel(list: ObservableList<GrupoDoPrincipal>, model: GrupoDoPrincipal): Int {
return list.indexOfFirst { it?.id == model?.id }
}
@InverseMethod("indexFromModel")
fun indexToModel(list: ObservableList<GrupoDoPrincipal>, index: Int): GrupoDoPrincipal {
return index.let { list[it] }
}
}
用法:
<Spinner
android:selectedItemPosition="@={Converters.indexFromModel(vm.allGrupoDoPrincipal, vm.principal.grupoDoPrincipal)}"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
此方法适用于Java:
public static int indexFromModel(ObservableList<GrupoDoPrincipal> list, GrupoDoPrincipal model) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == model.getId()) {
return i;
}
}
return -1;
}
@InverseMethod("indexFromModel")
public static GrupoDoPrincipal indexToModel(ObservableList<GrupoDoPrincipal> list, int index) {
return list.get(index);
}
最佳答案
在Kotlin中,object
方法不是真正的static
。为了使其静态,您需要为此方法添加注释@JvmStatic
:
object Converters {
@JvmStatic
fun indexFromModel(list: ObservableList<GrupoDoPrincipal>, model: GrupoDoPrincipal): Int {
return list.indexOfFirst { it?.id == model?.id }
}
@JvmStatic
@InverseMethod("indexFromModel")
fun indexToModel(list: ObservableList<GrupoDoPrincipal>, index: Int): GrupoDoPrincipal {
return index.let { list[it] }
}
}
关于android - 如何在Kotlin中制作双向转换器? (在Java中工作),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47857875/