我无法从下面的代码中读取 MAP 的 Arraylist 的值,请建议应该做什么。
@Override
protected void onPostExecute(Map<String,List<CommentModel>> results) {
super.onPostExecute(results);
results.get(0).get(commentModelList);
Toast.makeText(getApplicationContext(), "Helloji" + results, Toast.LENGTH_LONG).show();
}
以及添加到 Map 的值我认为以正确的方式将值放入 Map 中。请建议我该怎么做,因为我被困在这里。
String line = "";
StringBuffer buffer = new StringBuffer();
while ((line=bufferedReader.readLine())!=null){
buffer.append(line);
String result = buffer.toString();
JSONArray jsondata = new JSONArray(result);
commentModelList = new ArrayList<>();
commentModelList_child = new ArrayList<>();
map =new HashMap();
for(int i=0;i<=jsondata.length();i++){
CommentModel cmnmodel = new CommentModel();
JSONObject jsonObject = jsondata.getJSONObject(i);
int id= jsonObject.getInt("id");
String content = jsonObject.getString("content");
cmnmodel.setContent(content);
cmnmodel.setId(id);
commentModelList.add(cmnmodel);
commentModelList_child.add(cmnmodel);
map.put("commentModelList",commentModelList);
map.put("commentModelList_child",commentModelList_child);
}
}
最佳答案
您的Map
的键是String
而不是Integer
。
results.get(0).get(commentModelList);
不要将 Integer 作为键传递,而是使用 String 作为键,作为返回,您将获得 List。
List<CommentModel> comments = results.get("value_of_id");
关于java - java中如何从Map中读取Arraylist的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41812759/