android help
android help

Reputation: 59

Create a arraylist from output of a for loop

I am trying to get a data set as [{a,b,c,d} {e,f,g,h}] but I'm getting it as [a,b,c,d,e,f,g,h] using below mentioned code. What should I do to achieve that. any help will be highly appreciated. I'm new to android so please be kind enough to answer me. thanks in advance

List<String> CartItemEntityList = new ArrayList<String>();
for (int i = 0; i < mCartList.size(); i++) {

    if (!mCartList.isEmpty()) {
        Product product = mCartList.get(i);

        int quantity = product.quantity;
        double subTotal = product.subTotal;
        CartItemEntity = product.items;

        for (int j = 0; j < CartItemEntity.size(); j++) {
            Item item = CartItemEntity.get(j);
            subMenuCode = item.subMenuCode;
            mainMenuCode = item.mainMenuCode;
            price = item.price;
            CartItemEntityList.add(mainMenuCode);
            CartItemEntityList.add(String.valueOf(price));
            CartItemEntityList.add(String.valueOf(quantity));
            CartItemEntityList.add(subMenuCode);
        }
    }

    System.out.println("CartItemEntityList " + CartItemEntityList);
}

Upvotes: 0

Views: 59

Answers (2)

android help
android help

Reputation: 59

I was able to achieve this using json serialization and this is my answer

                              JSONObject jsonObj = new JSONObject();

                                jsonObj.put("MainMenuCode",
                                        item.getMainMenuCode());
                                jsonObj.put("Price", item.getPrice());
                                jsonObj.put("Quantity", product.getQuantity());
                                jsonObj.put("SubMenuCode",
                                        item.getSubMenuCode());

                                String a = jsonObj.toString();

                                CartIddtemEntityList.add(a);

Upvotes: 1

Sagar Maiyad
Sagar Maiyad

Reputation: 12733

you have to create array of arraylist. Try something like below:

ArrayList<ArrayList<String>> group = new ArrayList<ArrayList<String>>(mCartList.size());
for (int i = 0; i < mCartList.size(); i++) {
    List<String> CartItemEntityList = new ArrayList<String>();
    if (!mCartList.isEmpty()) {
        Product product = mCartList.get(i);
        int quantity = product.quantity;
        double subTotal = product.subTotal;
        CartItemEntity = product.items;
        for (int j = 0; j < CartItemEntity.size(); j++) {
            Item item = CartItemEntity.get(j);
            subMenuCode = item.subMenuCode;
            mainMenuCode = item.mainMenuCode;
            price = item.price;
            CartItemEntityList.add(mainMenuCode);
            CartItemEntityList.add(String.valueOf(price));
            CartItemEntityList.add(String.valueOf(quantity));
            CartItemEntityList.add(subMenuCode);
        }
    }       

    System.out.println("CartItemEntityList " + CartItemEntityList);
    group.add(CartItemEntityList);
}

Upvotes: 0

Related Questions