Android Question List of ToggleButtons

lip

Active Member
Licensed User
Longtime User
When i do this:

Code:
        Dim tbtnList As List
        tbtnList.Initialize
        tbtnList.AddAll(Array As ToggleButton (tbtn_EnOceanTopLeft,tbtn_EnOceanTopRight,tbtn_EnOceanBottomLeft,tbtn_EnOceanBottomRight))
        For Each tbtn As ToggleButton In tbtnList
        ...
        Next

I get this on execution of the "For Each..." line:
Log:
java.lang.ClassCastException: anywheresoftware.b4a.objects.CompoundButtonWrapper$ToggleButtonWrapper cannot be cast to android.widget.ToggleButton
 
Solution
Write so and it will work:
B4X:
    tbtnList.Add(tbtn_EnOceanTopLeft)
    tbtnList.Add(tbtn_EnOceanTopRight)
    tbtnList.Add(tbtn_EnOceanBottomLeft)
    tbtnList.Add(tbtn_EnOceanBottomRight)
    For Each tbtn As ToggleButton In tbtnList
        Log(tbtn.Enabled)
    Next

Why? I don't know, at the moment!

LucaMs

Expert
Licensed User
Longtime User
Write so and it will work:
B4X:
    tbtnList.Add(tbtn_EnOceanTopLeft)
    tbtnList.Add(tbtn_EnOceanTopRight)
    tbtnList.Add(tbtn_EnOceanBottomLeft)
    tbtnList.Add(tbtn_EnOceanBottomRight)
    For Each tbtn As ToggleButton In tbtnList
        Log(tbtn.Enabled)
    Next

Why? I don't know, at the moment!
 
Upvote 0
Solution

mangojack

Expert
Licensed User
Longtime User

Should be Array As Object

B4X:
    Dim tbtnList As List
    tbtnList.Initialize
    tbtnList.AddAll(Array As Object (tbtn_EnOceanTopLeft,tbtn_EnOceanTopRight,tbtn_EnOceanBottomLeft,tbtn_EnOceanBottomRight))
    For Each tbtn As ToggleButton In tbtnList
        Log(tbtn.Enabled)
    Next
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Yes but... why?
B4X:
'    Dim tbtnList As List
'    tbtnList.Initialize
''    tbtnList.AddAll(Array As ToggleButton (tbtn_EnOceanTopLeft,tbtn_EnOceanTopRight,tbtn_EnOceanBottomLeft,tbtn_EnOceanBottomRight))
'    tbtnList.Add(tbtn_EnOceanTopLeft)
'    tbtnList.Add(tbtn_EnOceanTopRight)
'    tbtnList.Add(tbtn_EnOceanBottomLeft)
'    tbtnList.Add(tbtn_EnOceanBottomRight)
'    For Each tbtn As ToggleButton In tbtnList
'        Log(tbtn.Enabled)
'    Next
    
'This works!
    Dim arrToggleButtons(4) As ToggleButton
    arrToggleButtons(0) = tbtn_EnOceanTopLeft
    arrToggleButtons(1) = tbtn_EnOceanTopRight
    arrToggleButtons(2) = tbtn_EnOceanBottomLeft
    arrToggleButtons(3) = tbtn_EnOceanBottomRight
    For Each tbtn As ToggleButton In arrToggleButtons
        Log(tbtn.Enabled)
    Next
 
Upvote 0

lip

Active Member
Licensed User
Longtime User
Amazing! Thank you friends on B4A Forum. Not sure what I did wrong, but looks like plenty of solutions. Thanks again.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…