1

I just configured some data in an ArrayList of HashMaps, which will be sent to another activity. There it will get decoded and the values are then stored in particular arrays. Let me show my code which I configured in the 1st Activity:

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

Now in 2nd Activity,

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[];*/

A big thanks, deeply appreciated.

1 Answer 1

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++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

now going to test,so first i have to iterate and then store !! ok?
right. hashmap doesnt give direct method to covert key value pairs into array.
it helped,that felt complete.Thanks for lightning response.
Raman,I have used one custom adapter with a listview to show result.My json file [ {blah},{nope}] had two objects.In list view two rows are showing but it showing duplicate.Means in list,it is showing only blah.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.