List of maps

wl

Well-Known Member
Licensed User
Longtime User
Hello,

In a library I wrote I create a List of Map objects and pass it to an event
The List itself is initialized as well as all Maps in this list.

In the B4A event I have something like:

B4X:
Sub pop_DownloadAllMessagesCompleted (Success As Boolean, Size As Int, Count As Int, Messages As List, headers As List)
      For lcv = 0 To Count -1
         Dim hdr As Map
         hdr = headers.Get(lcv)
         ListView1.AddSingleLine(hdr.Get("SUBJECT"))
      Next
End Sub

So I cast each item in the headers list as a Map.

In runtime on this line I get a class cast exception.

As a note: when I create a List of Maps in B4A all is OK.

Any idea ?

Thanks
 
Last edited:

warwound

Expert
Licensed User
Longtime User
In your library code are you creating and returning a anywheresoftware.b4a.objects.collections.List of anywheresoftware.b4a.objects.collections.Map or a java.util.List of java.util.Map?

I've found that returning a native Java object to B4A sometimes works but B4A may choke on some native Java objects so you should create and return the B4A objects instead.

Martin.
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Hi,

I was under the impression I was returning the Anywhere ... Map, this namespace was included, but I should check it again ...
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
You have to be explicit throughout.

The compiler may cast a native Android object to a B4A wrapper but it's not the same.

Look at this method from a library of mine, see how i've used fully qualified namespace to create and return a B4A List:

B4X:
public anywheresoftware.b4a.objects.collections.List GetTileSources() {
   anywheresoftware.b4a.objects.collections.List tileSources = new anywheresoftware.b4a.objects.collections.List();
   tileSources.Initialize();
   for (final ITileSource tileSource : TileSourceFactory.getTileSources()) {
      tileSources.Add(tileSource.name());
   }
   return tileSources;
}

And since you are dealing with a B4A List and not a java.util.List, you have to use B4A methods to Initialize and Add objects to the B4A List.

Martin.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…