Android Question Managing checkboxes in CustomListView

trueboss323

Active Member
Licensed User
Longtime User
Hello, I have a CustomListView and each line I have in the list has a checkbox that corrisponds to a setting. I use Statemanager to check if it’s true or false , then check/uncheck the box and move on to the next.
So instead of doing something like:
B4X:
If Statemanger.Getsetting(“key1”) = True Then
Checkbox1.checked = True
Else
Checkbox1.checked = False
End if

If Statemanger.Getsetting(“key2”) = True Then
Checkbox2.checked = True
Else
Checkbox2.checked = False
End if

If Statemanger.Getsetting(“key3”) = True Then
Checkbox3.checked = True
Else
Checkbox3.checked = False
End if

Can the whole thing be done in a loop instead? Something like:
B4X:
For Each chk as Checkbox in CustomListView
 

stevel05

Expert
Licensed User
Longtime User
If you add all of the checkboxes to a list or Array, you can iterate of the collection.

Something like:
B4X:
Dim Checkboxes As List = Array(CheckBox1,Checkbox2,Checkbox3) 'etc.

For Each CB as Checkbox in Checkboxes

...  do whatever

Next
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
If you add all of the checkboxes to a list or Array, you can iterate of the collection.

Something like:
B4X:
Dim Checkboxes As List = Array(CheckBox1,Checkbox2,Checkbox3) 'etc.

For Each CB as Checkbox in Checkboxes

...  do whatever

Next

This solves half of my problem , but how do I do it when I want to check for each specific setting? It’s not as simple as checking all the boxes at once but rather based on the Statemanger setting.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip:
B4X:
If Statemanger.Getsetting(“key1”) = True Then
Checkbox1.checked = True
Else
Checkbox1.checked = False
End if
Equivalent to:
B4X:
Checkbox1.Checked = Statemanger.Getsetting(“key1”) = True

You don't need to have references to all checkboxes.

It is difficult to post a solution without seeing your layout however if all items are built with the same layout then it is very simple to set the Checkboxes. For example to set Checkbox of item #2:
B4X:
'assuming that the checkbox index is 1 in the layout.
clv.GetPanel(3).GetView(1).Checked = Statemanger.Getsetting(“key2”) = True
 
Upvote 0

Similar Threads

Top