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

Discussion in 'Android Questions' started by MikeCLX, Apr 22, 2019.

  1. MikeCLX

    MikeCLX Member Licensed 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

    Code:
    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
     
  2. MikeCLX

    MikeCLX Member Licensed 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)
     
  3. Erel

    Erel Administrator Staff Member Licensed 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:
    Code:
    'when you create the item layout
    Panel1.LoadLayout(...)
    B4XSwitch1.Tag = Panel1
    You can then use it with:
    Code:
    Sub BtnSchedEnable_ValueChanged (Value As Boolean)
    Dim sw As B4XSwitch = Sender
    Dim MyIndex As Int =CLVPoolTC.GetItemFromView(sw.Tag)
     
  4. MikeCLX

    MikeCLX Member Licensed User

    thanks Erel,
    Interestingly that did not work either but if i set it to
    Code:
    B4XSwitch1.tag = Button1
    then it works like a charm.

    now i just need to tidy it up
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice