Java Question Return a list of maps

barx

Well-Known Member
Licensed User
Longtime User
I am wanting to return a list of maps from a library method. I have done the following:

B4X:
                List nodes = new List();
                nodes.Initialize();
                for (int i = 0; i < result.getNodes().size(); i++) {
                    Map node = new Map();
                    node.Initialize();
                    Node mNode = result.getNodes().get(i);
                   
                    node.Put("ID", mNode.getId());
                    node.Put("DisplayName", mNode.getDisplayName());
                   
                    nodes.Add(node);
                }

but get an error

java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap

Where did I go wrong?

Thanks
 

barx

Well-Known Member
Licensed User
Longtime User
in this Sub in B4A

B4X:
Sub DL_NodeResults(Results As List)
    Log("Connected Nodes: " & Results.Size)
    For i = 0 To Results.Size - 1
        Dim node As Map
        Log(Results.Get(i))
        node = Results.Get(i)
        Log(node.Get("ID"))
        Log(node.Get("DisplayName"))
    Next
End Sub

on
B4X:
node = results.Get(i)

Think I have sorted it now with

B4X:
nodes.Add(node.getObject()); //added.getObject()
 

barx

Well-Known Member
Licensed User
Longtime User
Me neither, we will see what big boss man says when he see's this. Th List seems to work fine.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
All objects that implement the ObjectWrapper interface are handled in a special way by the compiler.
Such objects are thin wrappers over the actual object.

B4X:
Dim m As Map
m.Initialize
Dim list1 As List
list1.Add(m) '<---- Add method expects an Object.
Dim m2 As Map = list1.Get(0) 'Get method returns an Object
If the wrapped object type doesn't exactly match the other type (Object in the above example) then the compiler gets (or sets) the inner object.
This means that the list should hold the inner object and not the wrapper.

Compile the above code in release mode and check the Java code. It will be more clear.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…