Android Question View cannot be converted to switchview

T201016

Active Member
Licensed User
Longtime User
Hi,
I am using class SwitchView. How to deal with the problem?

The compiler generates an error:

Compiling generated Java code. Error
B4A line: 541
If v Is SwitchView Then
javac 1.8.0_60
src\b4a\example\sampleOCR\preference.java:1406: error: incompatible types: View cannot be converted to switchview
if (_v.getObjectOrNull() instanceof b4a.example.sampleOCR.switchview) {
^

B4X:
Sub sw_CheckedChange(checked As Boolean, Index As Int)
    Private pnl As Panel
    pnl = CL4.GetPanel(Index)
    If Panel2.Visible Then
        For Each v As View In pnl.GetAllViewsRecursive
            If v Is SwitchView Then
                Dim sv As SwitchView = v
                sv.Checked = Not(checked)
            End If         
        Next
        Return
    End If
    ...
End Sub

And that is mentioned SwitchView class:

B4X:
'Class module "SwitchView"
#Event: CheckedChange (Checked As Boolean)
#RaisesSynchronousEvents: setChecked
#RaisesSynchronousEvents: Checked
Sub Class_Globals
    Private EventName As String
    Private CallBack As Object
    Public Switch As View
    Private joSwitch As JavaObject
End Sub

Public Sub Initialize (vCallback As Object, vEventName As String)
    EventName = vEventName
    CallBack = vCallback
    Switch = createSwitch
    joSwitch = Switch
End Sub

Private Sub createSwitch As View
    Dim so, cbo, ctxt As JavaObject
    ctxt = Me  
    ctxt = ctxt.RunMethodJO("getBA", Null)
    so.InitializeNewInstance("android.widget.Switch", Array As Object(ctxt.GetField("activity")))
    cbo.InitializeNewInstance("anywheresoftware.b4a.objects.CompoundButtonWrapper", Null)
    cbo.RunMethod("setObject", Array As Object(so))
    cbo.RunMethod("innerInitialize", Array As Object(ctxt, "switch", True))
    Return so
End Sub

Public Sub DesignerCreateView(base As Panel, lbl As Label, props As Map)
    Dim jo As JavaObject = base
    Dim parent As Panel = jo.RunMethod("getParent", Null)
    parent.AddView(Switch, base.Left, base.Top, base.Width, base.Height)
    base.RemoveView
    Switch.Tag = base.Tag
End Sub

Sub switch_CheckedChange(Checked As Boolean)
    CallSub3(CallBack, EventName & "_CheckedChange", Checked, Switch.Tag)
End Sub

Public Sub getChecked As Boolean
    Return joSwitch.RunMethod("isChecked", Null)
End Sub

Public Sub setChecked(b As Boolean)
    joSwitch.RunMethod("setChecked", Array As Object(b))
End Sub
'Gets or sets the "on" text.
Public Sub getTextOn As String
    Return joSwitch.RunMethod("getTextOn", Null)
End Sub
Public Sub setTextOn(s As String)
    joSwitch.RunMethod("setTextOn", Array As Object(s))
End Sub
'Gets or sets the "off" text.
Public Sub getTextOff As String
    Return joSwitch.RunMethod("getTextOff", Null)
End Sub
Public Sub setTextOff(s As String)
    joSwitch.RunMethod("setTextOff", Array As Object(s))
End Sub
 

T201016

Active Member
Licensed User
Longtime User
I close my topic. Problem solved ... (trivial mistake).

B4X:
Sub sw_CheckedChange(checked As Boolean, Index As Int)
    Private pnl As Panel
    pnl = CL4.GetPanel(Index)
    If Panel2.Visible Then
        Dim s_view As SwitchView = Sender
        s_view.Checked = Not(checked)
        Return
    End If
    ...
End Sub
 
Last edited:
Upvote 0
Top