Android Question get checkboxes in customlistview

andredamen

Active Member
Licensed User
Longtime User
I have made a custumlistview with personsname (col(0)), 2 checkboxes( p.LoadLayout("checkboxen")) and personid in col(4). When I read al the rows in the customlistview I get the personid with getvalue(x). So far good, but how can I read the personsname and de checks in the 2 checkboxes?

*********************************
CustomListView1.Add(CreateListItem(Col(0), CustomListView1.AsView.Width, 60dip),Col(4))

************************************

Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
Dim p As B4XView = XUI.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, Width, Height)
p.LoadLayout("checkboxen")
Label1.Text = Text
Return p
End Sub

****************************

For x = 0 To CustomListView1.Size -1
Log( CustomListView1.GetValue(x)) ' = deelnemerID

-->>How to get the chk of the 2 checkboxes?? is my question

Next

***************************
 

Mahares

Expert
Licensed User
Longtime User
How to get the chk of the 2 checkboxes?? is my question
You iterate thru the panels. Make sure you respect the designer tree views order:
B4X:
Private Sub Button1_Click
    For x = 0 To CustomListView1.Size -1        
        Dim p As B4XView=CustomListView1.GetPanel(x)
        Log($" ${x}  1st checkbox:  ${p.GetView(1).Checked)}"$  '2nd view in the items layout
        Log($" ${x}  2nd checkbox:  ${p.GetView(2).Checked)}"$  '3rd view in the items layout
    Next
End Sub
You can also solve this using a custom Type.
 
Upvote 0

andredamen

Active Member
Licensed User
Longtime User
Thansks Mahares.

I had 2 checkboxes and get it with p.getview(0) and p.getview(1). This works good.
Now I had to made 4 checkboxes. The program crashes when I want to set checkbox 3 and 4. The boxes are visible in the form. The fields in the database ar correct. I get the following in the log-file. I get the same error when I want to save the data. Can you or someone else tell me how to solve this?

tossdag_vulchecks (java line: 923)
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.CompoundButton
at anywheresoftware.b4a.objects.B4XViewWrapper.setChecked(B4XViewWrapper.java:290)
at andredamen.tossavonden.tossdag._vulchecks(tossdag.java:923)
at andredamen.tossavonden.tossdag._activity_create(tossdag.java:420)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at andredamen.tossavonden.tossdag.afterFirstLayout(tossdag.java:105)
at andredamen.tossavonden.tossdag.access$000(tossdag.java:17)
at andredamen.tossavonden.tossdag$WaitForLayout.run(tossdag.java:83)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

B4X:
    For x = 0 To CustomListView1.Size -1
        Dim p As B4XView = CustomListView1.GetPanel(x)

       DeelnemerID= CustomListView1.GetValue(x)
        txt = "SELECT * FROM " & Main.DBTableName4 & " where IDdeelnemer = '"& DeelnemerID & "'  and datum = '"&  Main.WelkeTossDatum & "' "
        Cursor1 = Main.SQL2.ExecQuery(txt)
       
        If Cursor1.RowCount <> 0 Then
           Cursor1.Position=0
           
            If Cursor1.GetInt("tijd1") = 0 Then p.GetView(0).Checked=False Else  p.GetView(0).Checked = True
            If Cursor1.GetInt("tijd2") = 0 Then p.GetView(1).Checked=False Else  p.GetView(1).Checked = True
            If Cursor1.GetInt("tijd3") = 0 Then p.GetView(2).Checked=False Else  p.GetView(2).Checked = True
            If Cursor1.GetInt("tijd4") = 0 Then p.GetView(3).Checked=False Else  p.GetView(3).Checked = True
           
           
     End If
    Next

B4X:
        For x = 0 To CustomListView1.Size -1
            Dim ListOfMaps As List
            ListOfMaps.Initialize
            Dim m As Map
            m.Initialize
               
            DeelnemerID= CustomListView1.GetValue(x)

            txt = "SELECT * FROM " & Main.DBTableName & " where IDnummer = '"& DeelnemerID & "' "
            Cursor1 = Main.SQL2.ExecQuery(txt)
            Cursor1.Position=0
            txt =  Cursor1.GetString("Naam")
           
            Dim p As B4XView = CustomListView1.GetPanel(x)

            m.Put("IDdeelnemer", DeelnemerID)
            m.Put("naam", txt)
            m.Put("datum", Main.WelkeTossDatum)
            m.Put("tijd1", p.GetView(0).Checked)
            m.Put("tijd2", p.GetView(1).Checked)
            m.Put("tijd3", p.GetView(2).Checked)
            m.Put("tijd4", p.GetView(3).Checked)
            ListOfMaps.Add(m)
            DBUtils.InsertMaps(Main.SQL1,Main.DBTableName4, ListOfMaps)
       
        Next
        Cursor1.Close
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
someone else tell me how to solve this?

Since it worked for you with 2 checkboxes but not 4 checkboxes, it is quite possible that the items tree order may be different from your code order. You may want to post a copy of your designer items tree picture. Someone in the forum may be able to pick up on it and help.
Also, off topic: It is preferable to use parameterized queries in your code syntax. You will not have to deal with those single quotes. Also, use resultset as opposed to cursor.
 
Upvote 0

andredamen

Active Member
Licensed User
Longtime User
Mahares. What you say is true: I have just found out myself. It start with a label and then 4 checkboxes. p.getview(3) seems to be the label. Now I know this, the whole problem is solved. Thanks for thinking with me.
 
Upvote 0
Top