Har*_*M V 11 android listview android-arrayadapter baseadapter
我有一个JSON字符串,其中包含以下多个实例
我想创建一个ListView,其中每个列表将具有上述内容.对于这种特殊情况,我需要扩展哪个更好的适配器来创建自定义适配器?
jks*_*der 10
我的假设是您在考虑任何一种情况之前已将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
}
}
Run Code Online (Sandbox Code Playgroud)
我强烈推荐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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10187 次 |
| 最近记录: |