B4J Question [Solved] How to get an object from a tag

Mark Read

Well-Known Member
Licensed User
Longtime User
I have a series of six buttons which all have almost the same code. They only use a different list. The lists are called f1 to f6. The list name I have put into the tag property but cannot get the code to call the proper list. The lists are dimensioned, initialised and filled in a different sub.

Pressing button1 should call list f1 in the sub below etc.

psudo: Get tag name (eg. f2) when button2 is pressed. Use list f2 in this sub.

Is this at all possible?

Sub:
Private Sub btnField_Click
    Dim f As List =Sender.As(B4XView).tag                                    '<-------------------------- Error here
    f.Initialize
    ' Initialize the single line data
    ' Remember to set the chart type in designer or use the type attribute
    SingleLine1.ClearData
 
    SingleLine1.Title = txt_name.Text
    SingleLine1.XAxisName = "Time"
    SingleLine1.YAxisName = Sender.As(B4XView).Text
    SingleLine1.IncludeLegend = "BOTTOM"
    SingleLine1.XScaleTextOrientation = "VERTICAL"
    SingleLine1.AddLine(Sender.As(B4XView).Text, xui.Color_Yellow)
    SingleLine1.AutomaticScale = False
  
    SortedData.Clear
    SortedData.AddAll(f)                                                    '<-------------------------- List
    SortedData.Sort(True)
    MinMax(SortedData.Get(0), SortedData.Get(SortedData.Size-1))

      
    For i=0 To ChartTime.Size-1
        SingleLine1.AddLinePointData(ChartTime.Get(i), f.Get(i), i Mod Xdivisions = 0)    '<-------------------------- List
    Next
    SingleLine1.DrawChart
End Sub

and the error I get is (as expected?):

Error:
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.

Error occurred on line: 198 (B4XMainPage)
java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.List (java.lang.String and java.util.List are in module java.base of loader 'bootstrap')
    at b4j.example.b4xmainpage._btnfield_click(b4xmainpage.java:739)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:234)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
 
Last edited:

Spavlyuk

Active Member
Licensed User
Yes it does and the name is what I need to cast to a list.
It's not possible to retrieve a variable by its name. If assigning the list to the tag doesn't work for you, maybe you can use a map instead.

B4X:
Dim L As List
L.Initialize2(Array(1, 2, 3))
Dim M As Map = CreateMap("Tag": L)
'...
Log(M.Get("Tag").As(List).Get(0)) 'Logs 1
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
You can't cast a String to a List but you can put the list itself in the Tag as the Tag can hold any object.

Agraham, thanks I didn't know that. That works like a charm.
 
Upvote 0
Top