0

i wanna retrieve map(ArrayList) according to the key, help me with the code below

public class MainActivity extends AppCompatActivity {
    Map<String,ArrayList<Model>> map=new ArrayMap<>();   <----

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     ......
         .....
     for (int j=0;j<jsonArray.length();j++)
        {
         JSONObject tempObj=jsonArray.getJSONObject(j);
         String price =tempObj.getString("price");
         String product=tempObj.getString("Product");
         String qty=tempObj.getString("Qty");
         modelList.add(new Model(id,price,product,qty,date));
         map.put(key,modelList);   <----
         modelList.clear();
         }
  ......
     ......
//here to retrieve that map in the same mainactivity
    CODE....???????     <----

here is my json where those months are dynamic (jan, april, jun,...they are not constant).

{
  "response": "success",
  "servicecode": "134",
  "forecast": {
    "month": {
      "jan": [
        {
          "id": "1",
          "price": "12",
          "Product": "1086",
          "Qty": "14"
        },
        {
          "id": "2",
          "price": "19",
          "Product": "1746",
          "Qty": "45"
        }
      ],
      "april": [
        {
          "id": "3",
          "price": "89",
          "Product": "1986",
          "Qty": "15"
        },
        {
          "id": "1",
          "price": "12",
          "Product": "1086",
          "Qty": "145"
        }
      ],
      "jun": [
        {
          "id": "81",
          "price": "132",
          "Product": "17086",
          "Qty": "1445"
        },
        {
          "id": "11",
          "price": "132",
          "Product": "10786",
          "Qty": "1445"
        }
      ]
    }
  },
  "message": "Competitor Sales."
}

what i did is i took all the response separately with the key and stored in MAp, now what i want to do is to display the array in according to month in View pager. so tell me map'll do good or any alternative....

kriss
  • 1,421
  • 1
  • 8
  • 16

3 Answers3

0

If you put ArrayMap correctly, you could get it as below:

Model model = map.get("april");
String id = model.id; 
String price = model.price;
.....

You should ensure the key variable in your codes is a month name.

navylover
  • 12,383
  • 5
  • 28
  • 41
0

Is the month the key of your map? If so, then you could simply define a list of months and retrieve the values accordingly, something like this:

for(String month: Arrays.asList("jan", "feb", "mar", "apr", "...")) {
    if(map.containsKey(month)) { //skip months if not present
        //--> code here
        Model model = map.get(month);
    }
}
ChopSuey
  • 9
  • 1
0

You're definitely on the right idea with maps, since you're able to assign each month an object.

However, you may want to consider using a LinkedHashMap instead of an ArrayMap because a LinkedHashMap preserves the insertion order, while ArrayMap's docs does not mention preserving insertion order anywhere.

The reason preserving insertion is important is because the months object in the JSON are presented in the order that you want to display it in. Therefore, if you parse "January" first, it will be guaranteed to be at the front of the Map.

You can declare and initialize a LinkedHashMap as follows:

Map<String, ArrayList<Model>> map = new LinkedHashMap<>();

I am assuming that you have correctly parsed this json into your objects, since you did not say anything in regards to the map being incorrectly populated, and by the placement of your arrows "<-----"

Now, for the code to actually use the Map.

for (String monthName : map.keySet()) {
    ArrayList<Model> modelArray = map.get(monthName);
    //now do whatever your view pager needs to do with this Model and its fields
    for (Model model : modelArray) {
        String price = model.getPrice(); //if you have a getter
        String product = model.product; //if the field is public
    }
}

Notes:

Using the LinkedHashMap's keyset is valid because the documentation guarantees that the keyset will be in insertion order. Also, this SO answer also confirms this fact.

I personally recommend GSON for parsing JSON objects, but since you seem to be okay with the parsing, this is not a big deal.

PhaseRush
  • 340
  • 4
  • 10