【发布时间】:2015-02-12 01:57:52
【问题描述】:
我有一个返回 JSON 数组的 PHP 脚本。数组中的每个 JSON 对象都是一个人,并具有姓名、电话号码、年龄等相关信息。
我想要做的是动态构建一个 ListView 来显示信息,其中列表的每个元素显示一个名字、一个电话号码和一个年龄。
现在我有循环遍历我的 JSON 数组的基本代码,但是如何设置我的适配器以显示这 3 条单独的信息?我知道如何显示单个项目,例如名称,但我不能 100% 确定如何创建一个可以显示 JSON 对象中的多个项目的适配器。
JSONArray json = new JSONArray(result);
setContentView(R.layout.activity_doctor_list);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getApplicationContext(), R.layout.doctor_item, listItems);
JSONObject doctor;
for(int i =0; i < json.length(); i++){
doctor = json.getJSONObject(i);
}
【问题讨论】: