Java ArrayList哈希映射提取并存储到ArrayList
我刚刚在HashMaps的ArrayList中配置了一些数据,这些数据将被发送到另一个活动。在那里它将被解码,然后将值存储在特定的数组中。让我展示我在第一个活动中配置的代码:Java ArrayList哈希映射提取并存储到ArrayList,java,android,arraylist,hashmap,Java,Android,Arraylist,Hashmap,我刚刚在HashMaps的ArrayList中配置了一些数据,这些数据将被发送到另一个活动。在那里它将被解码,然后将值存储在特定的数组中。让我展示我在第一个活动中配置的代码: ArrayList<HashMap<String, String>> hashMapArrayList = new ArrayList<HashMap<String, String>>(); JSONArray jsonArray = new JSONArray(cena);
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 hashMapArrayList=新建ArrayList();
JSONArray JSONArray=新JSONArray(cena);
对于(int i=0;iString[]price=newstring[hashMapArrayList2.size()];
String[]categoryName=新字符串[hashMapArrayList2.size()];
String[]productName=新字符串[hashMapArrayList2.size()];
String[]catalogName=新字符串[hashMapArrayList2.size()];
int i=0;
用于(映射项:HashMapArrayList 2){
价格[i]=项目获取(“价格”);
categoryName[i]=item.get(“categoryName”);
productName[i]=item.get(“productName”);
catalogName[i]=item.get(“catalogName”);
i++;
}
现在开始测试,所以首先我必须迭代然后存储!!好吗?对。hashmap没有提供将键值对转换为数组的直接方法。这很有帮助,感觉很完整。感谢lightning响应。Raman,我使用了一个带有listview的自定义适配器来显示结果。我的json文件[{blah},{nope}]有两个对象。在列表视图中显示两行,但它显示重复。表示在列表中,它只显示废话。
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[];*/
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++;
}