Android Question Pre-checking Check boxes in Customlistview

Kintara

Member
Licensed User
Longtime User
I am using Customlistview for the first time and wish to show a list of checkboxes.
Initially all list items are unchecked.
Each list item has the checked value saved to a map and eventually a file.
What I want to do is on subsequent creations of the Customlistview is set each list item's checkbox as it was saved last time it was shown.
I have tried to set the checked box by modifying the example code snippet
B4X:
Sub
For i = 1 To 10
        clv3.Add(CreateListItem("Item #" & i, True, clv3.AsView.Width, 50dip), 50dip, "Item #" & i)
    Next
end sub

Sub CreateListItem2(Text As String, Checked as Boolean, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    p.Color = Colors.Black
    Dim chk As CheckBox
    chk.Initialize("chk")
chk.checked=Checked
    p.AddView(chk, 380dip, 2dip, 50dip, Height - 4dip) 'view #0
    Return p
End Sub

I hit a problem that when I set
chk.checked=Checked
It fires the chk_CheckedChanged sub.

it thows an error in the class as there is no parent because nothing has been clicked.
Is there a way around this?
Can the checkbox be set in this way?


p.s. Running the CustomListView example code on its own activity works correctly - chk_CheckedChanged subroutine does not fire.

Kintara :cool:
 
Last edited:

eurojam

Well-Known Member
Licensed User
Longtime User
I have tried your example and it works more or less, see code below with some small changes
B4X:
sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    clv1.AddTextItem("Aaaa", "a")
    clv1.AddTextItem("Aaaa" & CRLF & "Bbbb", "b")
    clv1.AddTextItem("Aaaa" & CRLF & "Bbbb" & CRLF & "Cccc", "c")
    clv1.AddTextItem("Aaaa" & CRLF & "Bbbb" & CRLF & "Cccc" & CRLF & "Dddd" , "d")
    clv1.AddTextItem("Aaaa" & CRLF & "Bbbb" & CRLF & "Cccc" & CRLF & "Dddd" & CRLF & "Eeee", "e")
  
    'Second list is created programmatically.
    'Create 20 items made of a label, button and checkbox.
    clv3.Initialize(Me, "clv3")
    Activity.AddView(clv3.AsView, 0, 50%y, 100%x, 50%y)
    For i = 1 To 10
        'clv3.Add(CreateListItem("Item #" & i, clv3.AsView.Width, 50dip), 50dip, "Item #" & i)
        clv3.Add(CreateListItem2("Item #" & i, True, clv3.AsView.Width, 50dip), 50dip, "Item #" & i) 'call CreateListItem2
    Next
End Sub
Sub CreateListItem2(Text As String, Checked As Boolean, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    p.Color = Colors.Black
    Dim chk As CheckBox
    chk.Initialize("chk")
    chk.checked=Checked
    p.AddView(chk, 10dip, 2dip, 50dip, Height - 4dip) 'view #0, set x to 10dip to see the checkbox
    Return p
End Sub
Sub chk_CheckedChange(Checked As Boolean)
    Dim index As Int
    index = clv3.GetItemFromView(Sender)
    Dim pnl As Panel
    pnl = clv3.GetPanel(index)
    Dim chk As CheckBox
    chk = pnl.GetView(0) '<- changed this to 0, because only one view in the panel
    Msgbox("Item value: " & clv3.GetValue(index) & CRLF & "Check value: " & chk.Checked, "")
End Sub
 
Upvote 0

Kintara

Member
Licensed User
Longtime User
I found that it does work if the
B4X:
 For i = 1 To 10
        'clv3.Add(CreateListItem("Item #" & i, clv3.AsView.Width, 50dip), 50dip, "Item #" & i)
        clv3.Add(CreateListItem2("Item #" & i, True, clv3.AsView.Width, 50dip), 50dip, "Item #" & i) 'call CreateListItem2
    Next
is in the
B4X:
sub Activity_Create(FirstTime As Boolean)
part of the coding, but fails if it elsewhere.
Adding the example code as a new activity and calling it all works ok.
Just cant seem to get it to work if
B4X:
For i = 1 To 10
        'clv3.Add(CreateListItem("Item #" & i, clv3.AsView.Width, 50dip), 50dip, "Item #" & i)
        clv3.Add(CreateListItem2("Item #" & i, True, clv3.AsView.Width, 50dip), 50dip, "Item #" & i) 'call CreateListItem2
    Next
is called from elsewhere.
I think I will take your advice and see if I can replicate it from a much cut down version.

Thanks for now

Kintara :cool:
 
Upvote 0

Kintara

Member
Licensed User
Longtime User
Solved!

Found this by Mangojack in another thread which sees to solve the problem:
B4X:
Sub chkCheckAllItems_CheckedChange(Checked As Boolean)
   For i= 0 To clv3.GetSize -1  'CustomListView dimmed as clv3
     Dim pnl As Panel
     pnl = clv3.GetPanel(i)
  
     Dim chk As CheckBox
     chk= pnl.GetView(2)      'presuming checkbox is 3rd view in panel (same as example)
     chk.Checked = True
   Next
End Sub

I modified it to this and it solved my problem.
Note:
The CustomListview has to be created and filled with Panels first. Each panel has :
a checkbox (View0)
a label (View1)
and a button (View2)

B4X:
SUb SetCheckBoxes
Dim Cursor1 as cursor
Cursor1= SAMMSDatabase.ExecQuery("SELECT ID, State FROM Groups WHERE ID = '" & ID & "'")
For i = 0 to Cursor1.rowcount
Cursor.Position = 0
Dim State as String
State = Cursor1.GetValue2(1)
If State = "C" Then
Dim P as Panel
Dim Chk AS checkbox
chk = clv.GetPanel(i).GetView(0)
chk.Checked = True
end if
next
 
Upvote 0
Top