iOS Question Check all switches on CustomListView

Shay

Well-Known Member
Licensed User
I have CustomListView, in there I am putting X number of switches
I have another switch for "select all" (on different location on the page), so once I check it I wish all switches in CustomerListView will be either enabled or disabled
how do I make it?

B4X:
For i = 0 To ListCity.Size-1
      Dim pnl As Panel
      If pnl.IsInitialized = False Then
       pnl.Initialize("")
      End If
      pnl.Color = Colors.LightGray
      clv1.Add(CreateListItem(ListCity.Get(i), clv1.AsView.Width, 35dip,i), 35dip, i)
180dip,i), 180dip, i)
      clv1.Add(pnl, 3dip, i)
    Next
B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int, Num As Int) As Panel
    Dim p As Panel
    If p.IsInitialized = False Then
     p.Initialize("")
    End If
    p.Color = Colors.White
    Dim chk As Switch
    If chk.IsInitialized = False Then
     chk.Initialize("chk")
     chk.Tag = Num
    End If
    Dim lbl As Label
    If lbl.IsInitialized = False Then
     lbl.Initialize("")
    End If
    lbl.TextAlignment = lbl.ALIGNMENT_CENTER
    lbl.Font.CreateNew (10)
    lbl.Text = Text
    lbl.TextColor = Colors.Gray
    p.AddView(lbl, 5dip, 2dip, 100dip, Height - 1dip) 'view #0
    p.AddView(chk, 100dip, 2dip, 30dip, Height - 1dip) 'view #1
    Return p
   
End Sub
 

ilan

Expert
Licensed User
put this in your creatlistitem sub

B4X:
chk.Value = True 'is checked False not checked
 

Shay

Well-Known Member
Licensed User
I mean on my other switch (not related to the list item) if I enable it, I wish all my list item switches will be enabled
 

ilan

Expert
Licensed User
try this

B4X:
    For i = clv1.GetSize - 1
        Dim pnl As Panel
        pnl = clv1.GetPanel(i)
        Dim chk As Switch
        chk = pnl.GetView(1)
        chk.Value = True 'true = checked false = unchecked
    Next
 
Last edited:

Shay

Well-Known Member
Licensed User
this is what I tried, and got the following crash:
Error occurred on line: 82 (customlistview)
Expected: B4IPanelView, object type: UILabel
 

ilan

Expert
Licensed User
this is what I tried, and got the following crash:
Error occurred on line: 82 (customlistview)
Expected: B4IPanelView, object type: UILabel

this is because you are adding after you add the panel with the label and switch control another (empty) panel (it seems like you do it to Seperate the items)
and because this panel is empty and it doesnot got view(0) or (1) you get the error

set the background of the clv to Colors.LightGray and remove clv1.Add(pnl, 3dip, i)

B4X:
For i = 0 To ListCity.Size-1
     ' Dim pnl As Panel 'remove
      'If pnl.IsInitialized = False Then 'remove
    '   pnl.Initialize("") 'remove
     ' End If 'remove
    '  pnl.Color = Colors.LightGray 'remove
      clv1.Add(CreateListItem(ListCity.Get(i), clv1.AsView.Width, 35dip), 35dip, ListCity.Get(i))
      'clv1.Add(pnl, 3dip, i) 'remove
    Next

B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int, Num As Int) As Panel
    Dim p As Panel
     p.Initialize("")
    p.Color = Colors.White
    Dim chk As Switch
     chk.Initialize("chk")
     chk.Tag = Num
    Dim lbl As Label
     lbl.Initialize("")
    lbl.TextAlignment = lbl.ALIGNMENT_CENTER
    lbl.Font.CreateNew (10)
    lbl.Text = Text
    lbl.TextColor = Colors.Gray
    p.AddView(lbl, 5dip, 2dip, 100dip, Height - 1dip) 'view #0
    p.AddView(chk, 100dip, 2dip, 30dip, Height - 1dip) 'view #1
    Return p

End Sub
 

Shay

Well-Known Member
Licensed User
Thanks, but it is still crashing on:
pnl = clv1.GetPanel(i)
same crash
Maybe since I have more panels in this screen? (which I need them)
(but they are not in the same clv1)
 

ilan

Expert
Licensed User
Thanks, but it is still crashing on:
pnl = clv1.GetPanel(i)
same crash
Maybe since I have more panels in this screen? (which I need them)
(but they are not in the same clv1)
all panels that you add to your clv1 should have a label and a switch
if you are adding other panels to it then you will get the error

if you could post the code of the clv1 maybe it will help me (us) find the problem...
 

Brian Robinson

Active Member
Licensed User
I am not sure what the structure is of the Panels you are adding to the main Panel, and if you need to recursively for each panel update the switches, but I think this should work. Unfortunately I am not able to access my compiler at the moment, but you can start with this and I will have a look later today.

I had a look at the code for CustomListView and have made a change to the answer as it looks like the GetSize does not return the number of panels.

Add this code to the CustomListView
B4X:
Public Sub GetPanelCount
    Return panels.Size
End Sub
And the sub to update now looks like this.
B4X:
Private Sub UpdateSwitches(topView As CustomListView, updateVal As Boolean)
    For iLoop = 0 To topView.GetPanelCount - 1
        Dim pnl As Panel = topView.GetPanel(iLoop)
        For Each v As View In pnl.GetAllViewsRecursive
            If v Is Switch Then
                Dim sw As Switch = v
                sw.Value = updateVal
            End If
        Next
    Next
End Sub
 
Last edited:

Shay

Well-Known Member
Licensed User
I am not sure what the structure is of the Panels you are adding to the main Panel, and if you need to recursively for each panel update the switches, but I think this should work. Unfortunately I am not able to access my compiler at the moment, but you can start with this and I will have a look later today.

I had a look at the code for CustomListView and have made a change to the answer as it looks like the GetSize does not return the number of panels.
[/CODE]
Tried, still crashing
I attached the original code above
 

Brian Robinson

Active Member
Licensed User
Ok... after running up the code, I am not sure of how the function in the CustomListView is meant to work, but I changed the code to look like this:

Just commented out the bits I don't think need to be there
B4X:
Public Sub GetPanel(Index As Int) As Panel

'Dim p As Panel

Return Panels.Get(Index)

'p = Panels.Get(Index) 'this is the parent panel

'Return p.GetView(0)

End Sub
Oh yeah, I also updated your code so that the switches auto select and de-select when you change the switch. It should use the Value passed in.

B4X:
chk.Value = Value 'true = checked false = unchecked
You could also use the code I sent above as a neater solution:

B4X:
Sub Ckbox2_ValueChanged (Value As Boolean)

    UpdateSwitches(clv2, Value)

End Sub
If it still doesn't work then I have missed typing something here. If so, let me know and I will zip the project up ad upload it.

Cheers
Brian
 

Brian Robinson

Active Member
Licensed User
you have changed the CLV Class so i put back the old good original one and also added the switch in designer and everything is working
Ok. That makes sense. Looking through the CustomListView you send it a panel and it creates it's own panel which is why it was expecting a panel at index 0.

I didn't look at the original CustomListView class and so was confused by this comment:

B4X:
'create another panel to handle the click event

Pnl.SetLayoutAnimated(0, 1, 0, 0, sv.Width, ItemHeight)
when no extra panel was added.

Nice find Ilan.
 

Shay

Well-Known Member
Licensed User
Thanks a lot Ilan Brian
I changed the CustomListView due to this issue: (see if you can help with it, since now I got it again)
Change the array list to "A" to "C" only
and see the different color you have
since I am using dark colors, it looks bad...
 

Brian Robinson

Active Member
Licensed User
I don't have access to the compiler again, so can't see what you are talking about.

I would assume the problem there is that the panel is resizing to the number of items you add. I thought about changing the color of the ScrollView background to match the white of the panel, but I believe the scrollview colour is what determines the colour of the dividers.

Without having access to compiling the code to test any changes, you might be able to use the SetSize function to set the height of the scrollview, but I have a feeling you need to set the ContentHeight to match that of the ScrollView.

Cheers
Brian
 

Shay

Well-Known Member
Licensed User
yes you are right, it seems that I already solved it in the past, but since the new code changed I had to adjust the code to:
clv2.AsView.Height = (i * 36) + 1

Thanks Again
 
Top