[英]How to make the kotlin function with List<> callable from Java
In kotlin has MyClass with List member, and the builder has function to take the List<Data>. 在kotlin中,MyClass具有List成员,并且构建器具有获取List <Data>的功能。
but in Java code when it calls the builder's fun dataList(dataList: List<Data>)
it gets compiler error something about 但是在Java代码中,当它调用构建器的fun dataList(dataList: List<Data>)
它会得到有关以下内容的编译器错误
List<> cannot be applied to ArrayList<>
. List<> cannot be applied to ArrayList<>
。
From the decompiled code it shows the list is type of kotlin.collections.List<>
. 从反编译的代码中,它显示列表是kotlin.collections.List<>
类型。
How to make the function with List<> callable from Java? 如何使List <>的功能可从Java调用?
data class MyClass(
val context: Context,
val dataList: List<Data> = emptyList<Data>(),
) {
private constructor(builder: Builder) : this(builder.context, builder.dataList)
class Builder {
private var context: Context? = null
private var dataList: List<Data> = emptyList<Data>()
fun context(context: Context) = apply { this.context = context }
fun dataList(dataList: List<Data>) = apply { this.dataList = dataList }
fun build() = MyClass(this)
}
}
in JAVA 在JAVA中
ArrayList<Data> theDataList = new ArrayList<Data>();
theDataList.add(new Data())
MyClass myClass = new MyClass.Builder()
.context(appContext)
.dataList(theDataList) //<== error
.build();
the error is : 错误是:
dataList(Java.util.List<com.model.Data>) in Builder cannot be applied to dataList(Java.util.ArrayList<com.model.Data>)
and in Java code if clicking on the dataList() it opens the decompiled code as below, and seems it complains about the 在Java代码中,如果单击dataList(),则会按如下所示打开反编译的代码,并且似乎抱怨
fun dataList(dataList: kotlin.collections.List<com.model.Data>)
: fun dataList(dataList: kotlin.collections.List<com.model.Data>)
:
public final data class Data public constructor(context: android.content.Context,
dataList: kotlin.collections.List<com.model.Data> /* = compiled code */) {
public final val context: android.content.Context /* compiled code */
public final val dataList: kotlin.collections.List<com.model.Data> /* compiled code */
public final operator fun component1(): android.content.Context { /* compiled code */ }
public final operator fun component2(): kotlin.collections.List<com.model.Data> { /* compiled code */ }
public final class Builder public constructor() {
private final var context: android.content.Context? /* compiled code */
private final var dataList: kotlin.collections.List<com.model.Data> /* compiled code */
... ...
public final fun dataList(dataList: kotlin.collections.List<com.model.Data>): com.model.MyClass.Builder { /* compiled code */ }
... ...
public final fun build(): com.model.Data { /* compiled code */ }
}
Instead of 代替
ArrayList<Data> theDataList = new ArrayList<Data>();
you should use: 您应该使用:
List<Data> theDataList = new ArrayList<Data>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.