0

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);
}
2
  • 3
    You can use arraylist with in arraylist like...ArrayList<Arraylist<String>> Commented Jun 4, 2015 at 4:53
  • @gunjan luthra do u mind in giving any example Commented Jun 4, 2015 at 7:23

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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);
}

1 Comment

this resulted in something like group [[EC, 140.0, 1, AL], [EBC, 300.0, 1, AL]]. How can I get the curly brackets? (I don't need square brackets?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.