Android Question How can I cast "java.util.map" to a B4A Map?

fredo

Well-Known Member
Licensed User
Edit: While writing the post I messed up the thread title. It should read "How can I cast a B4A-Map to java.util.map?"

However, Erel helped to to find a solution (#6)to the real problem.



----- ----- ----- ----- -----
(I am trying to solve the problem of picture #2 from this, so I hope it's not a "duplicate" post)

While working with this library I get an error "Map cannot be cast to java.util.map"

#-btnSend_Click
#-submitPost, strTitle=Testtitle, strBody=Testmessage
testfibart_writenewpost (java line: 791)
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map cannot be cast to java.util.Map
at de.donmanfred.DatabaseReferenceWrapper.updateChildren(DatabaseReferenceWrapper.java:222)
at b4a.example.fibadb2.testfibart._writenewpost(testfibart.java:791)
at b4a.example.fibadb2.testfibart._submitpost(testfibart.java:732)
at b4a.example.fibadb2.testfibart._btnsend_click(testfibart.java:440)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21155)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Since I have no JAVA experience I was thinking that maybe a B4A-Map is different from a JAVA-Map.

While trying to make a cast function with Inline-JAVA I was not able to get it running because I don't know how to set the parameters etc.
21-08-_2016_13-39-07.jpg

What I need is someting like this:

B4X:
#If JAVA

public static castb4amaptojavamap(object mapB4A ) {
    import anywheresoftware.b4a.objects.collections.Map;

    Map m = new Map();
    m.Initialize();
    m = mapB4A;
    return m.getObject();
}
#End If

to use the function in B4A like:
B4X:
Dim childUpdates as Map
childUpdates.Initialize
childUpdates.Put("Key1", "testvalue1")

Private nativeMe As JavaObject
nativeMe.InitializeStatic(Application.PackageName & ".testfibart")
mDatabase.updateChildren( nativeMe.RunMethod("castb4amaptojavamap", childUpdates)   )
 
Last edited:

fredo

Well-Known Member
Licensed User
I guess that mapB4A is java.util.Map.

B4X:
m.Initialize();
m.putAll((java.util.Map)mapB4A);
Thanks for your answer, but I tried this and propably didn't get the syntax correct:
B4X:
#If JAVA

// Input parameter: B4A-map
// Output value:    java.util.Map with content of B4A-map

public static Map<String, String> map javamap(anywheresoftware.b4a.objects.collections.Map mapB4A ) {
    import anywheresoftware.b4a.objects.collections.Map;
    Map m = new Map();
    m.Initialize();
    m.putAll((java.util.Map)mapB4A);
    return m.getObject();
}

#End If

21-08-_2016_15-48-56.jpg
 

fredo

Well-Known Member
Licensed User
Try this:
Thank you, but I need it vice versa.

I have a B4A map:
B4X:
    Dim mapToConv As Map
    mapToConv.Initialize
    mapToConv.Put("key1", "val1")
    mapToConv.Put("key2", "val2")
...and need a javaMap
B4X:
    ' ...
    Dim mapToConv As Map
    mapToConv.Initialize
    mapToConv.Put("key1", "val1")
    mapToConv.Put("key2", "val2")

    ' ...
   
    mDatabase.updateChildren(  MapToJavamap(mapToConv)   )   
   
    '
So it would be: MapToJavamap(mapToConv)
 

fredo

Well-Known Member
Licensed User
So why does the title say: "How can I cast "java.util.map" to a B4A Map?" ????????????????

You don't need to do anything to cast a B4A map to java.util.map.

The problem is inside this method: de.donmanfred.DatabaseReferenceWrapper.updateChildren.
Oh, your are right. I have the title wrong, please excuse my mistake.
 

DonManfred

Expert
Licensed User
The problem is inside this method: de.donmanfred.DatabaseReferenceWrapper.updateChildren.
The firebase database reference is expecting a java.util.Map<String,String>

without doing much i only can get eclipse to accept this

B4X:
    public void updateChildren(Map map){
     
      java.util.Map<String, Object> mm = (java.util.Map<String, Object>) map;
            //for(int i=0; i<map.getSize(); i++)            {
            //    mm.put((String) map.GetKeyAt(i), map.GetValueAt(i));
            //}
      getObject().updateChildren(mm, new CompletionListener(){

                @Override
                public void onComplete(DatabaseError error, DatabaseReference paramDatabaseReference) {
                    // TODO Auto-generated method stub
                     if (ba.subExists(eventName + "_oncomplete")) {
                      BA.Log("lib:Raising.. "+eventName + "_oncomplete()");                               
                      //app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
                      ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_oncomplete", true, new Object[] {error.getCode(),error.getMessage()});
                  }else {
                      BA.Log("lib: NOTFOUND '"+eventName + "_oncomplete");
                  }

                }
      });
  }
map is the b4a Map where mm is the Map i give to firebase.

Is this code ok/correct?
 

fredo

Well-Known Member
Licensed User
Moved to lib thread


@DonManfred

Did you had a chance to try Erels solution from #10 above?

This lib is so extremely beneficial for the B4A world that I mean, it is worth to keep at it.

If you will make the changes on the library-side, I am willing to test the database functionality on the B4A-side and provide constant feedback to you.

If the library for the Firebase Realtime Database is at 100%, I'm happy to write a small tutorial for the Community.
 
Last edited:
Top