Out-projected type 'ArrayList<*>' prohibits the use of 'public open fun add(index: Int, element: E): Unit defined in java.util.ArrayList'

不打扰是莪最后的温柔 提交于 2019-11-30 11:14:21

Kotlin star-projections are not equivalent to Java's raw types. The star (*) in MutableList<*> means that you can safely read values from the list but you cannot safely write values to it because the values in the list are each of some unknown type (e.g. Person, String, Number?, or possibly Any?). It is the same as MutableList<out Any?>.

In contrast, MutableList<Any?> means that you can read and write any value from and to the list. The values can be the same types (e.g. Person) or of mixed types (e.g. Person and String).

In your case you might want to use dataList: MutableList<Any> which means that you can read and write any non-null value from and to the list.

So I have to cast to person like below:

val personList = (recyclerView.dataList as ArrayList<Person>)
personList.add( 0, Person("Lem Adane", "41 years old", 0))

because dataList is ArrayList<*> and not ArrayList and Kotlin is strict on that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!