B4J Question How Use b4x Map and List in Java

behnam_tr

Active Member
Licensed User
Longtime User
I have a list of products
I want to send it to a Java class
Now I don't know how to parse it on the Java side and retrieve all the information of a product with a loop

B4X:
    Dim list1 As List
    list1.Initialize
      
    Dim KalaMap As Map
    KalaMap.Initialize
    KalaMap.Put("stuffid","2720000000234")
    KalaMap.Put("name","pname1")
    KalaMap.Put("mu",164)
    KalaMap.Put("tedad",1)
    KalaMap.Put("Fee",10000)
    KalaMap.Put("Prdis",10000)
    KalaMap.Put("Dis",0)
    KalaMap.Put("Adis",10000)
    KalaMap.Put("Vra",0)
    KalaMap.Put("Vam",0)
    KalaMap.Put("Tsstam",10000)
    KalaMap.Put("Odam",0)
    KalaMap.Put("Olam",0)
      
    list1.Add(KalaMap)
  
  
    Dim KalaMap As Map
    KalaMap.Initialize
    KalaMap.Put("stuffid","2720000000234")
    KalaMap.Put("name","pname2")
    KalaMap.Put("mu",164)
    KalaMap.Put("tedad",1)
    KalaMap.Put("Fee",10000)
    KalaMap.Put("Prdis",10000)
    KalaMap.Put("Dis",0)
    KalaMap.Put("Adis",10000)
    KalaMap.Put("Vra",0)
    KalaMap.Put("Vam",0)
    KalaMap.Put("Tsstam",10000)
    KalaMap.Put("Odam",0)
    KalaMap.Put("Olam",0)
      
    list1.Add(KalaMap)
  
    //send to java
 
Last edited:
Solution
solved.

b4xList >> ArrayList
b4xMap >> HashMap

javaclass.parseList(list1)

B4X:
public void parseList(ArrayList  productsList){

        for (int i = 0; i < productsList.size(); i++) {

            System.out.println(productsList.get(i).toString());

            Map product = new HashMap((Map) productsList.get(i));

            String stuffid = product.get("stuffid").toString();
            String name =  product.get("name").toString();
            String mu =  product.get("mu").toString();
            String tedad =  product.get("tedad").toString();
            String Fee =  product.get("Fee").toString();
            String Prdis =  product.get("Prdis").toString();
            String Dis =  product.get("Dis").toString()...

stevel05

Expert
Licensed User
Longtime User
To get a comprehensive reply you will need to give more information as it sounds like you're not sure that is the way to go.

You don't say where you are sending it from, if it's B4j you could parse it in B4j Then send the resulting Map.

If you just want the java code, you could ask one of the available AI's I find Perplexity good for that.
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
To get a comprehensive reply you will need to give more information as it sounds like you're not sure that is the way to go.

You don't say where you are sending it from, if it's B4j you could parse it in B4j Then send the resulting Map.

If you just want the java code, you could ask one of the available AI's I find Perplexity good for that.

Thank you for checking this code. Now I want to access the list elements

Java:
 public void parseList(anywheresoftware.b4a.objects.collections.List productsList){

       System.out.println("list size:"+productsList.getSize());

        for (int i = 0; i < productsList.getSize(); i++) {

            System.out.println(productsList.Get(i).toString());

            anywheresoftware.b4a.objects.collections.Map product = (anywheresoftware.b4a.objects.collections.Map) productsList.Get(i);

            String stuffid = (String) product.Get("stuffid");
            String name = (String) product.Get("name");
            String mu = (String) product.Get("mu");
            String tedad = (String) product.Get("tedad");
            String Fee = (String) product.Get("Fee");
            String Prdis = (String) product.Get("Prdis");
            String Dis = (String) product.Get("Dis");
            String Adis = (String) product.Get("Adis");
            String Vra = (String) product.Get("Vra");
            String Vam = (String) product.Get("Vam");
            String Tsstam = (String) product.Get("Tsstam");
            String Odam = (String) product.Get("Odam");
            String Olam = (String) product.Get("Olam");
        }

    }


B4X:
Error On Line 9
java.lang.ClassCastException: class anywheresoftware.b4a.objects.collections.Map$MyMap cannot be cast to class anywheresoftware.b4a.objects.collections.Map (anywheresoftware.b4a.objects.collections.Map$MyMap and anywheresoftware.b4a.objects.collections.Map are in unnamed module of loader 'app')
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
solved.

b4xList >> ArrayList
b4xMap >> HashMap

javaclass.parseList(list1)

B4X:
public void parseList(ArrayList  productsList){

        for (int i = 0; i < productsList.size(); i++) {

            System.out.println(productsList.get(i).toString());

            Map product = new HashMap((Map) productsList.get(i));

            String stuffid = product.get("stuffid").toString();
            String name =  product.get("name").toString();
            String mu =  product.get("mu").toString();
            String tedad =  product.get("tedad").toString();
            String Fee =  product.get("Fee").toString();
            String Prdis =  product.get("Prdis").toString();
            String Dis =  product.get("Dis").toString();
            String Adis =  product.get("Adis").toString();
            String Vra =  product.get("Vra").toString();
            String Vam = product.get("Vam").toString();
            String Tsstam = product.get("Tsstam").toString();
            String Odam =  product.get("Odam").toString();
            String Olam = product.get("Olam").toString();

        }

    }
 
Upvote 0
Solution
Top