I have a List in my java file. java.util.List, and...
# announcements
f
I have a List in my java file. java.util.List, and i have a function in my kotlin class which add theese items to a mutableList. I get UnsupportedOperationException and i can"t solve it with stackoverFlow posts. What can I do?
d
How is
myMutableList
created? And can you show the stacktrace?
f
this is my adapter
Copy code
abstract class BaseRecyclerAdapter<T>(
       private var masterList: MutableList<T> = mutableListOf()
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
Copy code
fun updateList(list: List<T>) {
    masterList.clear()
    masterList.addAll(list as MutableList<T>)
    notifyDataSetChanged()
}
d
Why are you casting to
MutableList<T>
?
addAll
accepts a plain
List<T>
, no need for a mutable list.
f
2019-12-09 091955.307 12705-12705 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.velemjaro.app.debug, PID: 12705 java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) at java.util.AbstractCollection.addAll(AbstractCollection.java:344) at BaseRecyclerAdapter.updateList(BaseRecyclerViewAdapter.kt:12)
yes i know, but i try everything yes
d
And that error should be impossible to get with that code, because
mutableListOf
returns an
ArrayList
, which overrides
add
f
yet
so it was my original function
Copy code
fun updateList(list: List<T>) {
    masterList.clear()
    masterList.addAll(list)
    notifyDataSetChanged()
}
but i get this error every time
d
If you put a breakpoint at the start of that method, is
masterList
still an
ArrayList
?
f
Copy code
adapterBlog.updateList((List<BlogPost>) results);
masterList = {Collections$EmptryList@19456} size = 0
d
Are you assigning
masterList
anywhere else? Like
masterList = ...
anywhere?
f
just in the constructor
d
But then it can't be
Collections.EmptyList
...