Java Question List with Map

walterf25

Expert
Licensed User
Longtime User
Hello everyone, i need some help with something, I have the following code in Eclipse:

B4X:
  public List FindData(BA ba){
            IndexManager im = new IndexManager(ds);
            java.util.Map<String, Object> query = new HashMap<String, Object>();
            java.util.Map<String, Object> gt12 = new HashMap<String, Object>();
            gt12.put("$gt", 0);
            query.put("product_price", gt12);
          
            List list = new List();
            list.Initialize();
          
            QueryResult result = im.find(query);
          
            java.util.Map<String, Object>map1 = new HashMap<String, Object>();
            for (DocumentRevision revision : result) {
                Map map2 = new Map();   'B4A Map
                map2.Initialize();
                map1 = revision.getBody().asMap();   'Java.Util.Map
                for (Entry<String, Object> entry1 : map1.entrySet())
               
                {
                    map2.Put(entry1.getKey(), entry1.getValue());  
                }
                list.Add(map2);      'Add the map to a list
            }
          
            return list;
        }

the map that goes into the list looks something like this:
(MyMap) {product_description=Step out in style in this vibrant slim fit shirt by Peter England. This pure cotton shirt features a chest pocket, full sleeves and a crisp cut-away collar. Team it with smart slim fit trousers and formal shoes to look your best., product_size=[L, XL], product_name=Green long Sleeve Shirt, product_price=1399, product_features=[{BrandFitName=F-Slim Fit, Cuffs=Regular Cuff, Collar=Regular Collar, Sleeves=Full Sleeves, Fit=Slim Fit}], category_id=1, product_image=encoded image
}

I want to be able to retrieve from the list for example the Product_Description, product_size etc....
the problem is that i get the following error:
map1 = resultlist.Get(i)
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
at com.genesis.cloudantdb.main._deletedoc_click(main.java:534)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:4764)
at android.view.View$PerformClick.run(View.java:19844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap

Any ideas how to cast B4A Map to B4A MyMap or viceversa?

Thanks all!
Walter
 
Last edited:

walterf25

Expert
Licensed User
Longtime User
1. List hold objects.
2. The compiler doesn't know what is the type of each object.
3. Map is a wrapper object.
4. When the compiler needs to cast an unknown object to a wrapper type it assumes that the object stored is unwrapped.
5. You need to add map2.getObject() to the list instead of map2.
Got it Erel, thanks that did the trick

Walter
 
Top