Android Question How to get check box values from CustomListView.

microbox

Active Member
Licensed User
Longtime User
Hi everyone! I'm stuck in getting the status of the check box in the custom list view.
The code below calls the detailsdialog.
B4X:
Sub ShowItemdetailsDialog(ctr As Int, myList As List)
    Private cntr As Int = ctr
    Dim sf As Object = DetailsDialog.ShowAsync("Product list view records[" & ctr & "]", "", "Close", "", Null, False)
    DetailsDialog.SetSize(100%x, 86%y)
    Dim xuii As XUI
    Wait For (sf) Dialog_Ready(pnl As Panel)
    '--------------------------------------------Create Custom List View------------------------
    pnl.LoadLayout("cellItems0") ' Description
    preCreateCustList(ctr,myList)
    Wait For (sf) Dialog_Result (Result As Int)
    If Result = DialogResponse.CANCEL Then
        Log("Close")
    End If
Then calls preCreateCustList() to create the list.
B4X:
Sub preCreateCustList(cnt As Int, ml As List)
    clv2.Clear
    For i = 0 To cnt -1
        Private values1 As String
        values1 = ml.Get(i)
        clv2.Add(CreateListItem(values1, clv2.AsView.Width, 25%y), $"Item #${i}"$)
    Next
End Sub

B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As B4XView = xuii.CreatePanel("")
   
    p.SetLayoutAnimated(0, 0, 0, Width, Height)
    p.LoadLayout("cellItems")
    Log("p:"& p.NumberOfViews)
    Return p
End Sub
In the layout "cellItems" consist of labels and a checkbox, I'm trying the code below but I'm getting error (main_checkbox1_checkedchange (java line: 622)java.lang.RuntimeException: Object should first be initialized (View).)
B4X:
Sub CheckBox1_CheckedChange(Checked As Boolean)
    Dim index As Int = clv2.GetItemFromView(Sender)
    Dim pnl As B4XView = clv2.GetPanel(index)
    Dim chk As B4XView = pnl.GetView(1)
    Log($"Item value: ${clv2.GetValue(index)}Check value: ${chk.Checked}"$)
End Sub

Any input is appreciated...thanks.
 

DonManfred

Expert
Licensed User
Longtime User

Check the example and see how the checkboxes are accessed here
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
@DonManfred thank you. But I'm still having error. Just in case only..I attached the demo project I'm trying to solve.
Here where the error occur.
B4X:
Sub CheckBox1_CheckedChange(Checked As Boolean)
    Dim index As Int = clv2.GetItemFromView(Sender)
    Dim pnl As B4XView = clv2.GetPanel(index)
    Dim chk As B4XView = pnl.GetView(2)
    Log($"Item value: ${clv2.GetValue(index)}Check value: ${chk.Checked}"$)
End Sub
 

Attachments

  • xCustomListViewdemo.zip
    16.1 KB · Views: 161
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
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("cellItems")
    CheckBox1.Tag = Text
    'Label1.Text = Text
    Return p
End Sub

Sub CheckBox1_CheckedChange(Checked As Boolean)
    Dim chk As CheckBox = Sender
    Log($"Checkbox.Tag = ${chk.Tag}"$)
    Log($"Checkbox.Checked = ${chk.Checked}"$)
    'Dim index As Int = clv2.GetItemFromView(Sender)
    'Dim pnl As B4XView = clv2.GetPanel(index)
    
    'Dim chk As B4XView = pnl.GetView(2)
    'Log($"Item value: ${clv2.GetValue(index)}Check value: ${chk.Checked}"$)
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here where the error occur.
I am going to give you a different solution from Manfred, that is in line with your code and works You choose what works for you:
B4X:
Sub CheckBox1_CheckedChange(Checked As Boolean)
    Dim index As Int = clv2.GetItemFromView(Sender)
    Dim pnl As B4XView = clv2.GetPanel(index)
    Dim chk As B4XView = pnl.GetView(0).GetView(2)
    Log($"Item value: ${clv2.GetValue(index)}Check value: ${chk.Checked}"$)
End Sub
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Just a quick question...When I tick the checkbox I need to open a messagebox if the user select Yes it will continue otherwise I need to uncheck the checkbox. How do I toggle the checkbox status by code?
 
Upvote 0
Top