Android 如何在Kotlin中调用ArrayAdapter.addAll方法?
我有一个非常简单的列表(因此不需要创建扩展BaseAdapter的自定义适配器)。我使用ArrayUpdate来实现这一点 当我想更改适配器的值时,会出现一个生成错误,表示有两个allAll方法,kotlin不知道使用哪一个 以下是示例代码:Android 如何在Kotlin中调用ArrayAdapter.addAll方法?,android,kotlin,Android,Kotlin,我有一个非常简单的列表(因此不需要创建扩展BaseAdapter的自定义适配器)。我使用ArrayUpdate来实现这一点 当我想更改适配器的值时,会出现一个生成错误,表示有两个allAll方法,kotlin不知道使用哪一个 以下是示例代码: val list = Collections.emptyList<Any>() // Just for sample. val adapter = ArrayAdapter<Any>(this, android.R.
val list = Collections.emptyList<Any>() // Just for sample.
val adapter = ArrayAdapter<Any>(this, android.R.layout.simple_spinner_item, list) // This line is OK
// Some work later
adapter.clear()
adapter.addAll(list) // Here's the error
val list=Collections.emptyList()//仅用于示例。
val adapter=ArrayAdapter(这个,android.R.layout.simple\u spinner\u项目,列表)//此行正常
//以后再做一些工作
适配器。清除()
adapter.addAll(list)//下面是错误
错误消息是:
Error:(79, 35) Overload resolution ambiguity:
public open fun addAll(p0: (MutableCollection<out Any!>..Collection<Any!>?)): Unit defined in android.widget.ArrayAdapter
public open fun addAll(vararg p0: Any!): Unit defined in android.widget.ArrayAdapter
错误:(79,35)过载分辨率模糊:
public open fun addAll(p0:(MutableCollection..Collection?):在android.widget.ArrayAdapter中定义的单位
public open fun addAll(vararg p0:Any!):在android.widget.ArrayAdapter中定义的单元
如何解决它?在
ArrayAdapter
中有两种addAll
方法:
ArrayAdapter.addAll(T…项)
public void addAll(@NonNull Collection您应该使用String
而不是Any
您的代码应该是这样的
val list = Collections.emptyList<String>() // Just for sample.
val adapter = ArrayAdapter<String>(activity, R.layout.simple_spinner_item, list) // This line is OK
// Some work later
adapter.clear()
adapter.addAll()
val list=Collections.emptyList()//仅用于示例。
val adapter=ArrayAdapter(活动,R.layout.simple\u微调器\u项,列表)//此行正常
//以后再做一些工作
适配器。清除()
adapter.addAll()
添加扩展方法:
inline fun <T> ArrayAdapter<T>.addAllCollection(c: Collection<T>) = this.addAll(c)
非常感谢!非常有用!非常感谢!非常有用!
adapter.addAllCollection(list)