I have a very simple list (so it isn't necessary to create a custom adapter which extends BaseAdapter). I use an ArrayApdater to implements this.
When I want to change the value of the adapter, there's a build error says there's two allAll methods and kotlin don't know which one to use.
Here's the sample code:
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
The error message is :
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
How to solve it?
There are two addAll
methods in ArrayAdapter
:
ArrayAdapter.addAll(T ... items)
public void addAll(@NonNull Collection<? extends T> collection)
.so compiler is unable to infer which one method you want to call, because Any
is somewhat like Object
in Java.
Instead of :
val list = Collections.emptyList<Any>()
use val list = Collections.emptyList<String>()
.
And change ArrayAdapter<Any>
to ArrayAdapter<String>
Error should be resolved.
Hope it helps.
You should use String
instead of Any
Your code should be like this.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With