Android:从HashMap中提取字符串数组

时间:2014-01-22 18:40:30

标签: android json hashmap

我正在使用GridViewViewPager制作图库应用。我从JSON获取图像URL。我有正确显示图像的GridView,现在我正在转向ViewPager。我认为我需要做的是为ViewPager适配器生成图像URL的String数组。在GridView Activity中,我创建了一个HashMap,其中存储了id,URL和图像描述字符串。我现在的问题是:是否可以通过从HashMap中检索所有存储的URL字符串来生成字符串数组?我需要的是来自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" };

以下是我如何生成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;
    }

我对HashMap的工作原理有点不清楚,但我认为代码是通过JSON数组并将每个URL字符串拉出并存储在HashMap中,对吧?那么现在我可以拉出所有收集的URL字符串并用它们创建一个字符串数组吗?我该怎么做?

2 个答案:

答案 0 :(得分:0)

看起来很简单。填充完List<HashMap<String,String>>后,迭代每张地图并从中提取所需的值以添加到String[]。请参阅以下代码段:

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

答案 1 :(得分:0)

您可以尝试使用ArrayList并将URL添加到for循环中的ArrayList。循环完成后,您可以使用ArrayList.toArray(T [] array)方法将ArrayList中的URL复制到String数组。

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