【发布时间】:2012-11-15 16:10:48
【问题描述】:
如何在ArrayList 的HashMaps 中获取元素数组?我有一个带有 url 键的 HashMap。该值是一个 url 地址。几个HashMaps 存储在ArrayList 中。我想要的是一个带有所有 url 字符串的 array。我对找到的解决方案不满意,因为我认为它可以通过一些操作从 ArrayList 中提取出来。
// Hashmap for ListView
ArrayList<HashMap<String, String>> itemsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
jParser.execute(url);
try {
JSONObject json = jParser.get();
items = json.getJSONArray(TAG_ITEMS);
//This is the solution that I want to optimize
urls = new String[items.length()];
// looping through All items
for(int i = 0; i < items.length(); i++){
JSONObject c = items.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String description = c.getString(TAG_DESCRIPTION);
String author = c.getString(TAG_AUTHOR);
// Media is another JSONObject
JSONObject m = c.getJSONObject(TAG_MEDIA);
String url = m.getString(TAG_URL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_TITLE, title);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_AUTHOR, author);
map.put(TAG_URL, url);
// Solution
urls[i] = url;
// adding HashList to ArrayList
itemsList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
【问题讨论】:
标签: java android arrays arraylist hashmap