I used one of the answers from this question to get help with previous error but now I'm getting another one. There's a suggested answer to this question but I'm unable to get solution out of it for my problem.
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Object[]
private var data: Any? // fixed, can't change data type as it's in a compiled library to accept all kinds of data.
fun users() : ArrayList<User> {
return (data as Array<*>).filterIsInstance<User>() as ArrayList<User>
}
After the suggestions in the comment, the working code looks like this but I've another side effect, I can't add items to the array, the ArrayList remains empty.
fun users() : ArrayList<User> {
return (data as ArrayList<*>).filterIsInstance<User>() as ArrayList<User>
}
fun addItem(userVO: User) {
users().add(user)
}
Edit 2
val users: ArrayList<User> get() = ((data as? ArrayList<Any>)?.filterIsInstance<User>() ?: emptyList()) as ArrayList<User>
fun addItem(user: User) {
users.add(user)
}
data?var data: Any?, adding to my questionArrayif you storeArrayListindata?return (data as ArrayList<*>).filterIsInstance<User>() as ArrayList<User>filterIsInstancereturns a newArrayListto which you add youruser