android:从ArrayList中获取键值对

直接上示例:

public void getList(String cmdStr, int index, int type, ArrayList> mList) {
try {
JSONArray gethotlist = new JSONObject(cmdStr).getJSONArray("list");
JSONObject jso = (JSONObject)gethotlist.opt(index);
JSONArray jContent = jso.getJSONArray("content");
for (int j = 0; j < jContent.length(); j++) {
JSONObject mJContent = (JSONObject)jContent.opt(j);
HashMap map = new HashMap();
switch(type) {
case 0:
map.put("ItemTitle", getSecList(mJContent).name);
System.out.println("name:"+getSecList(mJContent).name);
break;
case 1:
map.put("ItemUrl", getSecList(mJContent).url);
map.put("ItemTitle", getSecList(mJContent).name);
System.out.println("url:"+getSecList(mJContent).url);
System.out.println("name:"+getSecList(mJContent).name);
break;
case 2:
map.put("ItemLen", getSecList(mJContent).len);
System.out.println("len:"+getSecList(mJContent).len);
break;
default:
break;
}
mList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void test() {
mDqJson.getList(cmdStr, index, GET_URL, urlList);
if (!urlList.isEmpty()) {
Iterator> it = urlList.iterator();
HashMap hash;
Object value = new Object();
Object nameValue = new Object();
int i = 0;
while(it.hasNext()) {
hash = it.next();
HashMap map = new HashMap();
value = hash.get("ItemUrl");
nameValue = hash.get("ItemTitle");
System.out.println("value-url:"+value.toString());
System.out.println("value-name:"+nameValue.toString());
}
}
}

你可能感兴趣的:(Android)