【发布时间】:2015-02-17 10:46:44
【问题描述】:
我有一个这样的 JSON:
[{"message":"Hello","id":1,"name":"Hello"},{"message":"James","id":2,"name":"Smith"}]
我用来填充ArrayList<HashMap<String, String>> 以便将json 的项目显示到ListView 中。
这是我目前使用的代码:
private void createListView(){
for(int i = 0 ; i < json.length() ; i++){
try{
JSONObject temp = json.getJSONObject(i);
HashMap<String, String> map = new HashMap<>();
map.put("name", temp.getString("name"));
map.put("message", temp.getString("message"));
data.add(map);
}catch (JSONException e){
e.printStackTrace();
}
}
String[] from = new String[]{"name", "message"};
int to[] = new int[]{android.R.id.text1};
ListAdapter adapter = new SimpleAdapter(getBaseContext(), data, android.R.layout.simple_list_item_1, from, to);
setListAdapter(adapter);
Log.d("Debug", json.toString());
}
这段代码几乎可以工作,但不是我想要的方式。它仅显示 name 字段。
如何修改它以同时显示 message 和 name 字段?
【问题讨论】:
-
它只显示名称字段,因为你给它显示 2 个项目并且你只声明 1 个位置来显示...
标签: java android json android-listview simpleadapter