我有一个JSON字符串,其中包含以下多个实例
- 名称
- 信息
- 时间戳
- 个人资料照片网址
我想创建一个ListView,其中每个列表都具有上述内容。对于这种特殊情况,我需要扩展哪个更好的适配器来创建自定义适配器?
我有一个JSON字符串,其中包含以下多个实例
我想创建一个ListView,其中每个列表都具有上述内容。对于这种特殊情况,我需要扩展哪个更好的适配器来创建自定义适配器?
我的假设是你在考虑任何一种情况之前已经将JSON字符串解析为对象模型...让我们调用这个类Profile。
策略1
ArrayAdapter足以使用覆盖的getView(..)方法,该方法将以您希望的方式将多个字段连接在一起。
ArrayAdapter<Profile> profileAdapter = new ArrayAdapter<Profile>(context, resource, profiles) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
// use the ViewHolder pattern here to get a reference to v, then populate
// its individual fields however you wish... v may be a composite view in this case
}
}
我强烈建议ViewHolder模式用于潜在的大型列表: https://web.archive.org/web/20110819152518/http://www.screaming-penguin.com/node/7767
它产生了巨大的变化。
策略2
或者,如果代表类上的toString()方法已经具有可接受显示的格式,则可以使用ArrayAdapter而不覆盖getView()。
这种策略更简单,但强制您将所有内容粉碎成一个字符串以供显示。
ArrayAdapter<Profile> profileAdapter = new ArrayAdapter<Profile>(context, resource, profiles)