B4J Question Adding a value to a list results in a UnsupportedOperationException

MathiasM

Active Member
Licensed User
Hello

I try to add a value to a list, but I get a java.lang.UnsupportedOperationException when I try to do this.
Why is this not working?

B4X:
Sub AppStart (Args() As String)
    TestList("Third", Array As String("First", "Second"))
End Sub

Sub TestList(NewValue As String, PreviousList As List)
    Dim NewList As List
    NewList.Initialize2(PreviousList)
    NewList.Add(NewValue)
End Sub

Thanks!
 

stevel05

Expert
Licensed User
Because Initialize2 creates a fixed size list. The same as if you do:
B4X:
    Dim l As List = Array("Test","Test")
    l.Add("Test")

You need to do:


B4X:
 Dim NewList As List
    NewList.Initialize
    NewList.AddAll(PreviousList)
    NewList.Add(NewValue)
 
Upvote 0
Top