kirktoon1882
kirktoon1882

Reputation: 1231

Android: Pulling a String Array from a HashMap

I'm building a a Gallery app using GridView and ViewPager. I'm getting the image URLs from JSON. I've got the GridView displaying the images correctly, and now I'm moving on to the ViewPager. What I think I need to do is generate a String array of the image URLs for the ViewPager Adapter. In the GridView Activity, I've created a HashMap with the id, URL, and image description strings stored. My question now is: Is it possible to generate a String Array by retrieving all the stored URL strings from the HashMap? What I need is this from the HashMap:

public static final String[] imagesStr = new String[] {
        "http://www.mysite/images/building0001.jpg",
        "http://www.mysite/images/building00011.jpg",
        "http://www.mysite/images/building0010.jpg" };

Here's how I'm generating the HashMap:

        @Override
    protected Void doInBackground(Void... params) {
        // Retrieve JSON Objects from the given URL address
        galleryArrList = new ArrayList<HashMap<String, String>>();
        jsonobject = JIJSONfunctions.getJSONfromURL(urlPathStr);

        try {
            // Locate the array name in JSON
            JSArrGallery = jsonobject.getJSONArray("gallery");

            for (int i = 0; i < JSArrGallery.length(); i++) {
                JSONObject galleryJO = JSArrGallery.getJSONObject(i);
                idStr = galleryJO.getString(TAG_ID);
                urlStr = galleryJO.getString(TAG_URL);
                descrStr = galleryJO.getString(TAG_DESCR);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String,String>();
                // adding each child node to HashMap key => value
                map.put(TAG_ID, idStr);
                map.put(TAG_URL, urlStr);
                map.put(TAG_DESCR, descrStr);

                galleryArrList.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

I'm a bit unclear on how a HashMap works, but I assume that the code is going through the JSON Array and pulling each URL string out and storing it in the HashMap, right? So now can I pull all the gathered URL Strings and create a String Array with them? How would I do that?

Upvotes: 0

Views: 245

Answers (2)

Darren
Darren

Reputation: 184

You might try using an ArrayList and add the URLs to the ArrayList within the for loop. When the loop is done, you can use the ArrayList.toArray(T[] array) method copy the URLs from the ArrayList to a String array.

ArrayList.toArray

ArrayList arlist = new ArrayList();
for (int i = 0; i < JSArrGallery.length(); i++) {
    JSONObject galleryJO = JSArrGallery.getJSONObject(i);
    idStr = galleryJO.getString(TAG_ID);
    urlStr = galleryJO.getString(TAG_URL);
    descrStr = galleryJO.getString(TAG_DESCR);

    // creating new HashMap
    HashMap<String, String> map = new HashMap<String,String>();
    // adding each child node to HashMap key => value
    map.put(TAG_ID, idStr);
    map.put(TAG_URL, urlStr);
    map.put(TAG_DESCR, descrStr);

    galleryArrList.add(map);

    arlist.add(urlStr);
}

// Create a String array to hold the URLs    
String[] arUrls = new String[arlist.size()];

// Copy the URLs from the ArrayList to the String array
arlist.toArray(arUrls);

Upvotes: 0

JuniKim
JuniKim

Reputation: 1067

Seems straightforward enough. After you have populated your List<HashMap<String,String>>, iterate over each map and pull the value you need from it to add to a String[]. Please refer to the following snippet:

galleryArrList = new ArrayList<HashMap<String, String>>();
jsonobject = JIJSONfunctions.getJSONfromURL(urlPathStr);
try { ... }

final int length = galleryArrList.size();
final String[] imagesStr = new String[length];
for (int i = 0; i < length; i++) {
    final Map<String, String> map = galleryArrList.get(i);
    imagesStr[i] = map.get(TAG_URL);
}

Upvotes: 0

Related Questions