Checkbox checked from database

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

What am I doing wrong?

A table contains some records that are listed in the svVKL.
When the record matches I want the CheckBox to be checked.

crCk = Wt.ExecQuery("Select Name From Table")
If crCK.RowCount > 0 Then

For i = 0 To svVKL.Panel.NumberOfViews -1
Dim cb As CheckBox
cb.Initialize("")
crVKL.Position = i
For j = 0 To crCK.RowCount -1
crCK.Position = j
If crCK.GetString("Name") = svVKL.Panel.GetView(i) Then
cb.Checked = True
End If
Next

Next

Thank you in advance.
Helen.
 

kickaha

Well-Known Member
Licensed User
Longtime User
The problem is here
B4X:
If crCK.GetString("Name") = svVKL.Panel.GetView(i) Then
Panel.GetView returns a view object, and will not match to a string.
I suspect that you have named the panels to correspond with the "Name" field of crCK, to use as a reference.

B4A does not return the name of a panel (or any other view), you need to work slightly differently:

1) When you declare the panel (either in the code or in designer), set the "Tag" property to the name of the panel.

2) Modify your code to
B4X:
crCk = Wt.ExecQuery("Select Name From Table")
If crCK.RowCount > 0 Then

For i = 0 To svVKL.Panel.NumberOfViews -1
Dim cb As CheckBox
cb.Initialize("")
Dim pnl As Panel 'you need a Panel to get at the Tag
pnl.Initialize ("") ' initialize the panel

crVKL.Position = i
For j = 0 To crCK.RowCount -1
crCK.Position = j
pnl = svVKL.Panel.GetView(i) 'Get the Panel that you want to test
If crCK.GetString("Name") = pnl.Tag Then 'compare the "Tag" value (which you have set to be the same as the panel name)
cb.Checked = True
End If
Next

Next
This should solve your problem.

Edit to add: I have assumed that the records that are listed in the svVKL are Panels, if not modify the code according to the view type
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Hello Kickaha,

First of all: thank you for your help plus the explanation.

I used this example to get a List with checkboxes running:
http://www.b4x.com/forum/basic4android-updates-questions/6816-checkbox-list.html

Now this line gives an error:
pnl = svVKL.Panel.GetView(i)

Error: java.lang.classcastexception:android.widget.checkbox

Helen.

In that case, as said in my edit you need to reference Checkbox:

When you declare the CheckBox (either in the code or in designer), set the "Tag" property to the name of the checkbox.

Then the code is:

B4X:
crCk = Wt.ExecQuery("Select Name From Table")
If crCK.RowCount > 0 Then

For i = 0 To svVKL.Panel.NumberOfViews -1
Dim cb As CheckBox
cb.Initialize("")

crVKL.Position = i
For j = 0 To crCK.RowCount -1
crCK.Position = j
cb = svVKL.Panel.GetView(i) 'Get the CheckBoxthat you want to test
If crCK.GetString("Name") = cb.Tag Then 'compare the "Tag" value (which you have set to be the same as the checkbox name)
cb.Checked = True
End If
Next

Next
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Helen,

I assume the code is still not working (from reading through it)

Try this version (changes are in bold)
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   Dim Wt As SQL
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim ItemHeight As Int
   ItemHeight = 40dip
   Dim svVKL As ScrollView
   Dim crVKL As Cursor
   Dim crCk As Cursor
   Dim bOK As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)

   svVKL.Initialize(0)

   Activity.AddView(svVKL, 0, 10, 100%x, 80%y)

   If Wt.IsInitialized = False Then
        Wt.Initialize(File.DirDefaultExternal, "wt.sql", False)
   End If
   
   crVKL = Wt.ExecQuery("Select Name From Table ORDER BY Name ASC")
   CreateList(crVKL.RowCount)
   
   Activity.LoadLayout("VKL")
'   Activity.Title("VKL")
   crVKL.Close
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub CreateList(NumberOfItems As Int)

   svVKL.Panel.Height = ItemHeight * NumberOfItems

   For i = 0 To crVKL.RowCount -1
   Dim cb As CheckBox
   cb.Initialize("")
      crVKL.Position = i
      cb.Text = crVKL.GetString("Name")
      svVKL.Panel.AddView(cb, 0, ItemHeight * (i-0), svVKL.Width, ItemHeight -2dip)
      Next
   
      
   crCk = Wt.ExecQuery("Select Name From VKL")
   If crCK.RowCount > 0 Then

      For i = 0 To svVKL.Panel.NumberOfViews -1
         Dim cb As CheckBox
         cb.Initialize("")
         [B]Dim pnl As Panel delete - not needed [/B]
         [B]pnl.Initialize("") delete - not needed [/B]
         crVKL.Position = i
         For j = 0 To crCK.RowCount -1
            crCK.Position = j
            [B]cb.svVKL = Panel.GetView(i)'use cb as the view is a checkbox [/B]

            [B]If crCK.GetString("Name") = cb.Text Then 'you can use the Text as you set it to "Name" above [/B]
               cb.Checked = True
            End If
         Next
      Next
   End If
   
End Sub
Sub bOK_Click

   Wt.ExecNonQuery("DELETE from VKL")
   Wt.ExecNonQuery("REINDEX VKL")
   
   Wt.BeginTransaction   
   For i = 0 To svVKL.Panel.NumberOfViews -1
      Dim cb As CheckBox
      cb = svVKL.Panel.GetView(i)
      If cb.Checked = True Then
         Wt.ExecNonQuery2("INSERT INTO VKL VALUES(NULL, ?)", Array As Object(cb.Text))
      End If
   Next
   Wn.TransactionSuccessful
   Wn.EndTransaction
   
   
End Sub
Sub bClear_Click

   For i = 0 To svVKL.Panel.NumberOfViews -1
   Dim cb As CheckBox
      cb.Initialize("")
      cb.Checked = False
   Next
   Wt.ExecQuery("DELETE from VKL")
   Wt.ExecQuery("REINDEX VKL")
   
End Sub
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Yeeeeh Kickaha,

You are fast.

Only changed cb.svVKL = Panel.GetView to
cb = svVKL.Panel.GetView

and finally my long lost checked checkboxes appeared.

You really made my day; thank you again.

Helen.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
You are most welcome - thanks for the feedback that has made MY day :sign0060:

Oh, and sorry about the typo !
 
Upvote 0
Top