Android Question CustomListView read checked items

Hello everyone, i have a problem with the clv...

im doing this to fill the clv and works perfect

B4X:
        clv_cob_referencias.Clear
        clv_cob_referencias.Add(CreateItemCO("dadadagr", "01/01/2020", "256.58"), refer)
        clv_cob_referencias.Add(CreateItemCO("hfththfthfth", "02/01/2020", "56.58"), refer)
        clv_cob_referencias.Add(CreateItemCO("sser54egdgdgr", "03/01/2020", "1256.58"), refer)
        clv_cob_referencias.Add(CreateItemCO("jyjgtrgse", "04/01/2020", "22346.58"), refer)
        clv_cob_referencias.Add(CreateItemCO("drhgdrgdr", "05/01/2020", "255.8"), refer)
        clv_cob_referencias.Add(CreateItemCO("dadawdawdaw", "06/01/2020", "2.58"), refer)
        clv_cob_referencias.Add(CreateItemCO("dadadagr", "01/01/2020", "256.58"), refer)
        clv_cob_referencias.Add(CreateItemCO("hfththfthfth", "02/01/2020", "56.58"), refer)
        
        
        
Private Sub CreateItemCO(refer As String, fecha As String, impo As String) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    Dim height As Int = 110dip
        
    If GetDeviceLayoutValues.ApproximateScreenSize < 4.5 Then height = 110dip
    p.SetLayoutAnimated(0, 0, 0, clv_cob_referencias.AsView.Width, height)
    p.LoadLayout("verrefer")
    
    p.Tag = refer
    lbl_cobr_desc1.Text = refer
    lbl_cobr_desc2.Text = fecha
    lbl_cobr_desc3.Text = impo
    chk_cobr_esta.Tag = impo
   
    
    lbl_cobr_desc1.TextSize = TextSizes
    lbl_cobr_desc2.TextSize = TextSizes
    lbl_cobr_desc3.TextSize = TextSizes
    
    Return p
End Sub

but when i need read all clv items to search which item is checked i get this error:

java.lang.RuntimeException: Object should first be initialized (View).

the error i get in this line: Dim chk As CheckBox = p.GetView(3)
the view 3 is the checkbox

I have this in a button:

B4X:
Sub btn_cobr_prueba_Click

    For i = 0 To clv_cob_referencias.GetSize - 1
        Dim p As Panel = clv_cob_referencias.GetPanel(i)
        Dim chk As CheckBox = p.GetView(3)
        If chk.Checked Then
            Log("checked...")
        End If
    Next

End Sub

Thanks all
Regards
 
Hi Erel, this is the full error and is in this line Dim chk As CheckBox = p.GetView(3)

Error occurred on line: 13875 (Main)
java.lang.RuntimeException: Object should first be initialized (View).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4a.objects.B4XViewWrapper.GetView(B4XViewWrapper.java:317)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7352)
at android.widget.TextView.performClick(TextView.java:14177)
at android.view.View.performClickInternal(View.java:7318)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27801)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
 
Upvote 0
ok... i tell you what im doing.
im using tabstrip with 3 tabs. in the first i have the clv filled with database items. i check the items i need and change to the tab 2. in the tab 2 i have a button that do this
B4X:
Sub btn_cobr_prueba_Click

    For i = 0 To clv_cob_referencias.GetSize - 1
        Dim p As Panel = clv_cob_referencias.GetPanel(i)
        Dim chk As CheckBox = p.GetView(3)
        If chk.Checked Then
            Log("checked...")
        End If
    Next

End Sub

in the tab 2 the user enter data and i need check the data versus the items in the clv. thats all.
Maybe now you can help me.
 
Upvote 0

rraswisak

Active Member
Licensed User
clv_cob_referencias.GetSize - 1
I don't think CustomListView (xCLV) has GetSize method 🤔
1600331991403.png
 
Upvote 0
Try to create a small example that demonstrates the problem and upload it.

here it is. This is a small part of a larger project, so the code is kind of ugly. but it serves the function of showing the error. Sure it's something simple, but I can't find it.
the error appear when click the button in the second tab

Thanks all
 

Attachments

  • example.zip
    15.7 KB · Views: 115
Upvote 0
Perfect. Now it works as I need.
My confusion started with this code:
because here I don't need .GetView(0)

B4X:
Sub pnl_cobr_refer_Click
    Dim p As Panel
    Dim chk As CheckBox
    p = Sender
  
    chk = p.GetView(3)
    If chk.Checked = True Then
        chk.Checked = False
    Else
        chk.Checked = True
    End If
End Sub

and here yes

B4X:
Sub btn_cobr_prueba_Click
  
    For i = 0 To clv_cob_referencias.GetSize - 1
        Dim p As B4XView = clv_cob_referencias.GetPanel(i)
        Dim chk As CheckBox = p.GetView(0).GetView(3)
        If chk.Checked Then
            Log("si" & i)
        End If
    Next

End Sub

what is the difference?


as always you are a genius. thanks for helping me. thanks all too.
regards.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
what is the difference?
There is a panel inside a panel. In the first case you are getting the nested panel and in the second case the parent panel.

This is how I would have wrote the first sub:
B4X:
Sub pnl_cobr_refer_Click
    Dim p As B4XView = Sender
    Dim chk As B4XView = p.GetView(3)
    chk.Checked = Not(chk.Checked)
End Sub

Cross platform and more elegant :)
 
Upvote 0
ahhhhh... now i understand.

so now i need convert something like this:
Dim p As Panel
Dim chk As CheckBox

in this:
Dim p As B4XView
Dim chk As B4XView

in all my proyect no?

and when i define items with the designer, they also have to be B4XView too.
 
Upvote 0
Top