我正在使用GridView
和ViewPager
構建一個圖庫應用程序。我從JSON獲取圖片網址。我已經讓GridView正確顯示圖像,現在我正在轉向ViewPager。我想我需要做的是爲ViewPager適配器生成一個包含圖像URL的String數組。在GridView Activity中,我創建了一個HashMap
,其中存儲了id,URL和圖像描述字符串。我現在的問題是:是否可以通過從HashMap中檢索所有存儲的URL字符串來生成一個字符串數組?我需要的,這是從HashMap中:Android:從哈希映射中抽取字符串數組
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字符串拉出來,並用它們創建一個字符串數組?我會怎麼做?
您可以使用列表本身 '字符串URL = galleryArrList.get(0)獲得(TAG_URL);' – Raghunandan