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

Discussion in 'Android Questions' started by fredo, Aug 21, 2016.

  1. fredo

    fredo Active 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:

    Code:
    #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:
    Code:
    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: Aug 22, 2016
  2. Erel

    Erel Administrator Staff Member Licensed User

    I guess that mapB4A is java.util.Map.

    Code:
    m.Initialize();
    m.putAll((java.util.Map)mapB4A);
     
  3. fredo

    fredo Active Member Licensed User

    Thanks for your answer, but I tried this and propably didn't get the syntax correct:
    Code:
    #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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    There are several mistakes in your code.

    Try this:
    Code:
    Sub JavaMapToMap (javamap As Object) As Map
       
    Dim m As Map
       m.Initialize
       
    Dim mo As JavaObject = m
       mo.RunMethod(
    "putAll"Array(javamap))
       
    Return m
    End Sub
     
    DonManfred and MarcoRome like this.
  5. fredo

    fredo Active Member Licensed User

    Thank you, but I need it vice versa.

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

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

    Erel Administrator Staff 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.
     
    fredo likes this.
  7. fredo

    fredo Active Member Licensed User

    Oh, your are right. I have the title wrong, please excuse my mistake.
     
  8. DonManfred

    DonManfred Expert Licensed User

    The firebase database reference is expecting a java.util.Map<String,String>

    without doing much i only can get eclipse to accept this

    Code:
    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((
    Stringmap.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 likes this.
  9. fredo

    fredo Active Member Licensed User

    With my humble JAVA picoknowlegde I would say "yes".

    But don't the comment slashes prevent the transfer from "map" to "mm"?

    21-08-_2016_18-16-34.jpg
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    It should be:
    Code:
    java.util.Map<String, Object> mm = (java.util.Map<String, Object>) map.getObject();
     
  11. fredo

    fredo Active 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: Sep 1, 2016
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice