简体   繁体   中英

how to get two string array item position and store in each separate string array in android

Sorry for my bad English. I am using two separate string array for each item of arraylist. All things working fine . My Question is I want to take each item of String array (say) if i take string array "asasaqty" and another item of string array "asasaId" then I want to store each item of string array value in third string array (say:[id1,qty1] and so on). Please let me know if there is a proper solution for this.

Below is my code.

Thanks in advance

String [] asasaqty;
    String [] asasaId;

    final AddToCartData addToCartData = response.body();
                    shoppingBagArrayList = addToCartData.getData();

                    asasaqty = new String[shoppingBagArrayList.size()];
                    asasaId = new String[shoppingBagArrayList.size()];


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


                        asasaqty[i] = shoppingBagArrayList.get(i).getQty();

                     asasaId[i]=shoppingBagArrayList.get(i).getProductId();


                    System.out.println("==========wwwww========"+ Arrays.toString(asasaqty));
                    System.out.println("======wwwwssssswwwwss======"+ Arrays.toString(asasaId));

                   /* HashMap<String,String> map=new HashMap<String,String>();
                    for (int i = 0; i < asasaqty.length; i++) {
                   map.put(asasaqty[i],asasaId[i]);
                        System.out.println("======aaaaaaa======"+ map.put(asasaqty[i],asasaId[i]));

                    }*/

After printing the value in logcat the value is looking like

/System.out: ==========wwwww========[3, 2]
/System.out: ======wwwwssssswwwwss======[151, 10]

Now i want to take another String array and store each item asasaqty and asasaId in separate array like below

String[] xyz = [3,151] and 
String[] abc = [2,10]

Please let me know how can we do this.

You can use productID and key (Assuming it is unique) and qty as value.

 HashMap<Integer,Integer> dataMap = new HashMap<>();

    for (int i=0; i <shoppingBagArrayList.size; i++){
        dataMap.put(shoppingBagArrayList.get(i).getProductId() , shoppingBagArrayList.get(i).getQty());
    }

    System.out.println("DataMap: "+dataMap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM