在ListView控件设置JSON响应 [英] Setting JSON response in ListView
问题描述
如果(状态== 200){
字符串结果= EntityUtils.toString(response.getEntity());
Log.d(响应,结果);
ArrayAdapter<字符串>适配器=新ArrayAdapter<字符串> (getActivity(),
android.R.layout.simple_list_item_1,结果);
setListAdapter(适配器);
}
我无法设置我的反应到我的列表。我得到我的答复 {
约会:[{结果
名称:凯文,
},{
名称:约翰,
}]
}
我怎么会为我名的参数列表视图。
我怎么会为我名的参数列表视图。
块引用>一目了然。
ArrayAdapter
的第三个参数应该是集合,但你只传递简单的字符串。根据你的JSON
我建议你:
- 创建自己的对象,将重新present 人与属性(名称,
年龄等)- 解析
JSON
并创造者的集合- 设置集合ListAdapter
示例:
公共类Person { 私人字符串名称;
... // getter和setter @覆盖
公共字符串的toString(){
返回this.name;
}
}然后,你需要解析
JSON
,并得到正确的数据:列表<&人GT;人=新的ArrayList<&人GT;();
人人= NULL;
JSONObject的根=新的JSONObject(sourceString);
JSONArray约会= root.getJSONArray(约会);
JSONObject的孩子= NULL;
的for(int i = 0; I< appointments.length();我++){
子= appointments.getJSONObject(ⅰ);
如果(孩子!= NULL)
人=新的Person();
person.setName(child.getString(姓名));
persons.add(人);
}
}然后初始化
ListAdapter
:新ArrayAdapter<串GT;(getActivity(),布局,人);
和现在你得到了它。
注意:
原因为什么我重写
的toString()
方法是,因为使用的是默认的ArrayAdapter与内置的Android的布局列表中的每个对象将被转换为字符串并将在ListView中显示,但如果我不覆盖的toString()
它不会返回人的名字,但再串对象的presentation是人类不可读字符串。if (status == 200) { String result = EntityUtils.toString(response.getEntity()); Log.d("Response", result); ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(), android.R.layout.simple_list_item_1, result); setListAdapter(adapter); }
I am unable to set my response into my List. I get my response as
{ "Appointments": [{
"Name": "Kevin", }, { "Name": "John", }] }How would i set my name parameter in the listView.
解决方案How would i set my name parameter in the listView.
At a glance. Third parameter of
ArrayAdapter
should be collection but you are passing only simple String. According to yourJSON
i recommend you:
- Create own object that will represent Person with properties(name, age, etc.)
- Parse
JSON
and create collection of persons- Set collection to ListAdapter
Example:
public class Person { private String name; ... // getters and setters @Override public String toString() { return this.name; } }
Then, you need to parse
JSON
and get proper data:List<Person> persons = new ArrayList<Person>(); Person person = null; JSONObject root = new JSONObject(sourceString); JSONArray appointments = root.getJSONArray("Appointments"); JSONObject child = null; for (int i = 0; i < appointments.length(); i++) { child = appointments.getJSONObject(i); if (child != null) person = new Person(); person.setName(child.getString("Name")); persons.add(person); } }
Then initialise
ListAdapter
:new ArrayAdapter<String>(getActivity(), layout, persons);
and now you got it.
Note:
Reason why i'm overriding
toString()
method is that since you are using default ArrayAdapter with build-in Android layout each object in your List will be converted to String and will be shown in ListView but if i don't overridetoString()
it won't return person's name but string representation of object that is human-unreadable string.这篇关于在ListView控件设置JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!