Java Question How to convert Object to Map?

adolleschal

Member
Licensed User
Longtime User
I have a Library which returns an Array of Objects, and now i want to convert this array to a Map. In Java i could simply do

Map<String,String> object = (Map<String,String>) myObject[y];

but how can i do something like that in BA4?

Thanks.

Best regards
Andreas
 

adolleschal

Member
Licensed User
Longtime User
Thank you. That was my first idea how to do that, but when I code that i get the Error:

java.lang.ClassCastException: java.util.HashMap

Whats wrong?

Best regards
Andreas
 

jaminben

Member
Licensed User
Longtime User
Hi all,

After a mamouth search this is the best thread that I've found that comes close to doing what I'm trying to do.

I have some data which I'd like to put into a Map (HashMap?)... the data would look something like:

myMap(Key, Array As Object(Value1, Value2, Value3)

or

myMap(1, Array As Object("Name", "Job", Bitmap)

So basically I need to create a map with a key and for that key to return 3 results (2 strings and a Bitmap).

The best that I can do is get it to semi work by returning an object which I then can't access to get the values inside that object.

How can I do this?

:sign0104:
 

derez

Expert
Licensed User
Longtime User
If I understand correctly -
You can define a type which has the three objects, and fill the map with objects of that type.
Getting an object from the map by the key, you can refer to each of its internal objects.
 

jaminben

Member
Licensed User
Longtime User
If I understand correctly -
You can define a type which has the three objects, and fill the map with objects of that type.
Getting an object from the map by the key, you can refer to each of its internal objects.

Could you explain in a little more detail as I'm not understanding types all that well.

Ok, so I've worked out this so far:

B4X:
Sub Process_Globals
Type personDetails(Name As String, Character As String, Profile As Bitmap)
End Sub

Sub JobDone (Job As HttpJob)

Dim pD As personDetails
         
pD.Profile = Job.GetBitmap
pD.Name = castNameDataStore.Get(Job)
pD.Character = characterNameDataStore.Get(Job)
         
castMapTest.Put("0", pD)

End Sub

Sub castListView_ItemClick (Position As Int, Value As Object)
Log("castMapTest: " & castMapTest.Get("0"))
End Sub

This returns:

B4X:
castMapTest: [Character=Hot Girl, Profile=(Bitmap): 100 x 150, Name=9 - Carmen Electra, IsInitialized=false]

Now I just need to work out how to get the individual results (Character, Profile and Name).
 
Last edited:

stefanobusetto

Active Member
Licensed User
Longtime User
B4X:
Sub castListView_ItemClick (Position As Int, Value As Object)
Dim pD As personDetails
pD = castMapTest.Get("0")

Log("castMapTest: " & pD.Name)
End Sub
 
Last edited:

jaminben

Member
Licensed User
Longtime User
B4X:
Sub castListView_ItemClick (Position As Int, Value As Object)
Dim pD As personDetails
pD = castMapTest.Get("0")

Log("castMapTest: " & pD.Name)
End Sub

Thanks for that :)

Here's the last peice of my code just in case anyone else needs to see it for reference or can spot any errors.

B4X:
Sub JobDone (Job As HttpJob)
   
   Select Job.JobName
      Case "castProfileImages"
   
         If Job.Success = True Then
         
            Dim pD As personDetails
            
            pD.Profile = Job.GetBitmap
            pD.Name = castNameDataStore.Get(Job)
            pD.Character = characterNameDataStore.Get(Job)
            
            castCollection.Put(castPersonIDStore.Get(Job), pD)
            
         Else
         
            Dim pD As personDetails
            
            pD.Profile = peoplePoster
            pD.Name = castNameDataStore.Get(Job)
            pD.Character = characterNameDataStore.Get(Job)
            
            castCollection.Put(castPersonIDStore.Get(Job), pD)

         End If
         
         castCountDown = castCountDown + 1
         
         If castCountDown = castPersonIDList.Size Then
         
            For i = 0 To castPersonIDList.Size - 1
               
               Dim pD As personDetails
               pD = castCollection.Get(castPersonIDList.Get(i))
               
               castListView.AddTwoLinesAndBitmap(pD.Name, pD.Character, pD.Profile)
         
            Next
         
         End If
         
   End Select
   
Job.Release

End Sub
 

walterf25

Expert
Licensed User
Longtime User
Hello gentlemen, i'm glad i found this thread, i have a similar issue, i'm wrapping a library in Eclipse and i have came across the need of converting a java.util.Map<String, Object> to B4A map, i'm not sure how to do this, I know i can convert a B4A map to a java.util.Map<String, Object> by doing something like this
B4X:
Map.getObject();

but i need to do the opposite now, here's a snippet of the code i'm dealing with.
B4X:
        public void UpdateRecord(BA ba, BasicDocumentRevision retrieved, Map map) throws DocumentException{
            MutableDocumentRevision update = retrieved.mutableCopy();
            java.util.Map<String, Object> json = retrieved.getBody().asMap();
            'I need to do something like map = json;
            'but obviously i get the famous error telling me i B4A map can not be cast to java.util.hashmap
            update.body = DocumentBodyFactory.create(map.getObject());
            ds.updateDocumentFromRevision(update);
        }

Any thoughts or suggestions will be greatly appreciated.

Thanks,
Walter
 

walterf25

Expert
Licensed User
Longtime User
B4X:
java.util.Map javaMap = map.getObject();

It will warn about missing generic type. Ignore the warning.
Thanks Erel.

Walter
 
Top