Android Question Any way to use SwitchView class on CustomListView?

Andris

Active Member
Licensed User
Longtime User
I'm successfully using CustomListView and it's great. However, I've added a SwitchView (class) as one of the views, and although it appears nicely on the display, I can't capture the CheckedChange event. This is the CheckedChange code:

B4X:
Sub sw_CheckedChange(Checked As Boolean)   
    Dim index As Int
    index = clv.GetItemFromView(Sender)
    Dim pnl As Panel
    pnl = clv.GetPanel(index)
    Dim sv As SwitchView  
    sv = pnl.GetView(2)
    Msgbox("Switch: " & index & CRLF & "Position: " & sv.Checked, "")
End Sub

The problem is that SwitchView is not recognized as an Android view so it crashes at sv=pnl.GetView(2). Is there something I should be doing to eliminate or get around this problem, or is a class like Switchview just not usable with CustomListView?
 

DonManfred

Expert
Licensed User
Longtime User
so it crashes at sv=pnl.GetView(2)
... with which error?

Check the contents of pnl after getting it with
B4X:
pnl = clv.GetPanel(index)
B4X:
Sub sw_CheckedChange(Checked As Boolean) 
    Dim index As Int
    index = clv.GetItemFromView(Sender)
    Dim pnl As Panel
    pnl = clv.GetPanel(index)
    For Each v As View In pnl.GetAllViewsRecursive
            Log(v)
        Next
        'Dim sv As SwitchView
    'sv = pnl.GetView(2)
    'Msgbox("Switch: " & index & CRLF & "Position: " & sv.Checked, "")
End Sub
IS the Switchview the 3rd childview?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Small addition...
B4X:
Sub sw_CheckedChange(Checked As Boolean)  
    Dim index As Int
    index = clv.GetItemFromView(Sender)
    Dim pnl As Panel
    pnl = clv.GetPanel(index)
    For Each v As View In pnl.GetAllViewsRecursive
            Log(v)
            If v Is SwitchView Then
                Dim sv As SwitchView = v
                ' do whatever
            End If           
        Next
        'Dim sv As SwitchView 
    'sv = pnl.GetView(2)
    'Msgbox("Switch: " & index & CRLF & "Position: " & sv.Checked, "")
End Sub
 
Upvote 0

Andris

Active Member
Licensed User
Longtime User
Small addition...
B4X:
Sub sw_CheckedChange(Checked As Boolean) 
    Dim index As Int
    index = clv.GetItemFromView(Sender)
    Dim pnl As Panel
    pnl = clv.GetPanel(index)
    For Each v As View In pnl.GetAllViewsRecursive
            Log(v)
            If v Is SwitchView Then
                Dim sv As SwitchView = v
                ' do whatever
            End If          
        Next
        'Dim sv As SwitchView
    'sv = pnl.GetView(2)
    'Msgbox("Switch: " & index & CRLF & "Position: " & sv.Checked, "")
End Sub

Thanks Manfred. Yes, SwitchView is definitely the 3rd item. If I use a CheckBox in place of SwitchView, all works perfectly. I have a feeling that the problem is similar to the one discussed here and I will play with Erel's suggested code to see if I can make it work (but will have to wait until after the holidays!). Thanks for your input and have a great Holiday Season!
 
Upvote 0

Andris

Active Member
Licensed User
Longtime User
Thanks Manfred. Yes, SwitchView is definitely the 3rd item. If I use a CheckBox in place of SwitchView, all works perfectly. I have a feeling that the problem is similar to the one discussed here and I will play with Erel's suggested code to see if I can make it work (but will have to wait until after the holidays!). Thanks for your input and have a great Holiday Season!

Also this discussion thread ...
 
Upvote 0
Top