Android Question XCustomVistView cannot click on B4XSwitch and get the current panel in the list

MikeCLX

Member
Licensed User
Longtime User
this is the code, button1 works but the B4XSwitch does not (BtnSchedEnable)

is there a way to get the index with GetItemFromView on a B4XSwitch? or any other means?

Regards,

Mike

B4X:
Sub BtnSchedEnable_ValueChanged (Value As Boolean)
    Try
        Dim MyIndex As Int =CLVPoolTC.GetItemFromView(Sender)
        ToastMessageShow(MyIndex,False)
        Dim TCi As TCItem = CLVPoolTC.GetValue(MyIndex)
        TCi.Tue.TextColor= 0xff6a5acd'
    Catch
        Log(LastException)
    End Try
End Sub
Sub Button1_Click
    Try
        Dim MyIndex As Int =CLVPoolTC.GetItemFromView(Sender)
        ToastMessageShow(MyIndex,False)
        Dim TCi As TCItem = CLVPoolTC.GetValue(MyIndex)
        TCi.Tue.TextColor= 0xff6a5acd'
      
    Catch
        Log(LastException)
    End Try
End Sub
 

MikeCLX

Member
Licensed User
Longtime User
this is the error i get:

192.168.20.212 Error ReadAnalogs
Error occurred on line: 1109 (Main)
java.lang.ClassCastException: b4a.example.b4xswitch cannot be cast to android.view.View
at anywheresoftware.b4a.objects.B4XViewWrapper.asViewWrapper(B4XViewWrapper.java:69)
at anywheresoftware.b4a.objects.B4XViewWrapper.getParent(B4XViewWrapper.java:166)
at b4a.example3.customlistview._getitemfromview(customlistview.java:408)
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$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:742)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Don't use toast messages for debugging. Use Log instead.
2. The Try / Catch blocks are not needed and will only make it more difficult to fix the problem.

A custom view class is never a view by itself.
Unfortunately the base view of B4XSwitch is not public. I will change it for a future version.
You can instead do something like:
B4X:
'when you create the item layout
Panel1.LoadLayout(...)
B4XSwitch1.Tag = Panel1

You can then use it with:
B4X:
Sub BtnSchedEnable_ValueChanged (Value As Boolean)
Dim sw As B4XSwitch = Sender
Dim MyIndex As Int =CLVPoolTC.GetItemFromView(sw.Tag)
 
Upvote 0

MikeCLX

Member
Licensed User
Longtime User
thanks Erel,
Interestingly that did not work either but if i set it to
B4X:
B4XSwitch1.tag = Button1

then it works like a charm.

now i just need to tidy it up
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…