androidlinkedhashmapcopyonwritearraylist

How to convert arraylist to array?


I have an arraylist with JSON values. I want to convert it to an array, then I will send it to MapActivity. I tried a lot of ways but I failed.

for (int i = 0; i < matchFixture.length(); i++) 
{
    JSONObject c = matchFixture.getJSONObject(i);
    String matchId = c.getString(TAG_MATCHID);
    Log.d("matchId", matchId);

    //  hashmap for single match
    HashMap<String, String> matchFixture = new HashMap<String, String>();

    // adding each child node to HashMap key => value
    matchFixture.put(TAG_MATCHID, matchId);

    matchFixtureList.add(matchFixture);

    //item=matchFixtureList.get(0).toString();
    String[] stockArr = matchFixtureList.toArray(new String[matchFixtureList.size()]);
    stockArr = matchFixtureList.toArray(stockArr);
    for(String s : stockArr)
        System.out.println(s);
}

Solution

  • You are trying to typecast a HashMap to a String Your map is -

     //  hashmap for single match
        HashMap<String, String> matchFixture = new HashMap<String, String>();
        // adding each child node to HashMap key => value
        matchFixture.put(TAG_MATCHID, matchId);
    

    And you are adding matchFixture (which is a map) to an ArrayList (which should be a list of HashMaps)

    matchFixtureList.add(matchFixture);
    

    Now if you want to convert matchFixtureList to an array then you should do like this -

        HashMap<String,String>[] stockArr = new HashMap[matchFixtureList.size()];
        stockArr = matchFixtureList.toArray(stockArr);
        for(HashMap<String,String> map : stockArr)
           for(Map.Entry<String,String> entry : map.entrySet()){
               Log.d("debug", entry.getKey() + ","+ entry.getValue());
           }