繁体   English   中英

如何将arrayList存储为Map'string作为键以及如何检索它。

[英]how to store a arrayList to Map 'string as key and how to retrieve it..!

我想根据键检索map(ArrayList),请使用以下代码帮助我

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....???????     <----

这是我的json,这些月份是动态的(jan,pril,jun,...它们不是恒定的)。

{
  "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."
}

我所做的是我将所有响应分别与键分开保存在MAp中,现在要做的是在View pager中根据月份显示数组。 所以告诉我map'll做得好或任何其他选择。

如果正确放置ArrayMap,则可以如下所示:

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

您应该确保代码中的key变量是月份名称。

月是您地图的关键吗? 如果是这样,那么您可以简单地定义一个月份列表并相应地检索值,如下所示:

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

使用地图绝对是正确的主意,因为您可以每月分配一个对象。

但是,您可能要考虑使用LinkedHashMap而不是ArrayMap因为LinkedHashMap保留插入顺序,而ArrayMap的文档没有提到在任何地方都保留插入顺序。

保留插入之所以重要很重要,是因为JSON中的months对象是按照您想要显示的顺序显示的。因此,如果首先解析“ January”,则可以保证它位于Map的前面。

您可以按以下方式声明和初始化LinkedHashMap:

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

我假设您已正确将此json解析为您的对象,因为您未对未正确填充地图以及通过放置箭头“ <-----”说什么

现在,让代码实际使用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
    }
}

笔记:

使用LinkedHashMap的密钥集是有效的,因为文档保证密钥集将按插入顺序进行。 同样, 这个SO答案也证实了这一事实。

我个人建议使用GSON解析JSON对象,但是由于您似乎对解析没有问题,所以这没什么大不了的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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