ArrayList Hash Map提取并存储到arraylist中

时间:2016-05-11 11:21:15

标签: java android android-studio arraylist hashmap

我刚刚在HashMaps的ArrayList中配置了一些数据,这些数据将被发送到另一个活动。在那里它将被解码,然后将值存储在特定的数组中。让我展示我在第一个活动中配置的代码:

ArrayList<HashMap<String, String>> hashMapArrayList = new ArrayList<HashMap<String, String>>();
JSONArray jsonArray = new JSONArray(cena);

for(int i=0; i<jsonArray.length(); i++) {

    //ointer exception
    jsonObject = jsonArray.getJSONObject(i);

    pr = jsonObject.getString("price");
    cgN = jsonObject.getString("categoryName");
    pN = jsonObject.getString("productName");
    cN = jsonObject.getString("catalogName");

    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("Price", pr);
    hashMap.put("CategoryName", cgN);
    hashMap.put("ProductName", pN);
    hashMap.put("CatalogName", cN);

    hashMapArrayList.add(hashMap);

    Intent intent = new Intent(Login.this, Checkout.class);
    //I took bundle but type convention error was coming
    intent.putExtra("arrayhash", hashMapArrayList);
    startActivity(intent);
}

现在进入第二个活动,

ArrayList<HashMap<String, String>> hashMapArrayList2 = 
    (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arrayhash");

// Now here I am confused with extracting and also after extracting storing it like this:

/*String price[];
  String categoryName[];
  String productName[];
  String catalogName[];*/

非常感谢,非常感谢。

1 个答案:

答案 0 :(得分:1)

String[] price = new String[hashMapArrayList2.size()];
String[] categoryName = new String[hashMapArrayList2.size()];
String[] productName = new String[hashMapArrayList2.size()];
String[] catalogName = new String[hashMapArrayList2.size()];

int i=0;
for (Map<String, String> item : hashMapArrayList2) {
    price[i] = item.get("Price");
    categoryName[i] = item.get("CategoryName");
    productName[i] = item.get("ProductName");
    catalogName[i] = item.get("CatalogName");
    i++;
}