How do I get this out from datamodel recycler view in andorid using volley?

Viewed 29

ArrayList arr = new Arraylist<>();

CartData class consist
 String food_name,
 String qty,
 String order_id

public void setFood(String foodName) {

    this.food_name = foodName;
}
public String getFood() {

     return food_name;
}

public void setQty(String qty) {

    this.qty = qty;
}
public String getQty() {

     return qty;
}

public void setOrderId(String orderId) {

    this.order_id = orderId;
}
public String getOrderId() {

     return order_id;
}

I'm getting data like this

{
    "cart_data": [
        {
            "food_name: "Dosa",
            "order_id": "66",
            "qty": "5", 
        }
        {
            "food_name: "Samosa",
            "order_id": "66",
            "qty": "10", 
        },
        {
            "food_name: "Chapathi",
            "order_id": "2",
            "qty": "1", 
        },
        {
            "food_name: "Biriyani",
            "order_id": "38",
            "qty": "11", 
        },
        {
            "food_name: "Samosa",
            "order_id": "38",
            "qty": "10", 
        }
    ],
   
}

I want to display data like this in the recycler view

1st Order

  1. Dosa, 5
  2. Samosa, 10

2nd order

  1. Chapathi, 1

3rd order

  1. Biriyani, 11
  2. Samosa, 10

The grouping is done with order id, ie., if items having same order id will be grouped together, if only have one item. It also grouped as seperately. Like above example.(See 2nd order for this)

Also I want to know How i needed to call adapter.

1 Answers

Since it is grouped on the basis of order id,

You json should be of the structure:

{
   "orders":[
      {
         "order_id":"66",
         "cart_data":[
            {
               "food_name":"Dosa",
               "order_id":"66",
               "qty":"5"
            },
            {
               "food_name":"Samosa",
               "order_id":"66",
               "qty":"10"
            }
         ]
      },
      {
         "order_id":"2",
         "cart_data":[
            {
               "food_name":"Chapathi",
               "order_id":"2",
               "qty":"1"
            }
         ]
      }
   ]
}

Since, the json structure has items listed separately, You'll have to modify the structure at model level. Your ideal model should look like this:

class Order {
    String orderId;
    List<CartData> cartDataItems;
}

class CartData {
    String food_name;
    String qty;
    String order_id;
}

Step 1: Serialize your json into List<CartData>

Step 2: Create another list List<Order> and add the data into it from List<CartData> prepared in Step 1

Step 3: Pass this list List<Order> (from Step 2) to your adapter

Let me know if you have any questions !

Related