Android——自定义ArrayAdapter
2015-11-20 20:44
405 查看
在取得网页端传回的JSON数据并封装成对象数据后,想要把对象数据的信息显示在一个ListView中,在这里使用自定义ArrayAdapter给ListView添加adapter。
首先传回的JSON数据格式为:
很清楚一个键为”userinfo“值为JSON数组的对象
JSON数组里的对象包含四对Name-Value
所以我们的对象是一个User包含上面的四个字段:uername,id,age,sex;
User.java还有一个静态方法用来将JSON字符串解析为User对象数组
通过User 的静态方法 getUserArrayFromJSONString()可以得到一个List。
下面通过自定义ArrayAdapter显示List的数据
新建一个类UserArrayAdapter,继承自ArrayAdapter,(超类没有无参构造函数)必须为其显示添加一个构造方法调用超类的构造方法
这里采用和超类一样的三个基本参数,Context context, int resource, List objects
接下来是最关键的一步,就是重写基类的getView方法,让他返回一个自定义的ListView Item的View,
首先通过LayoutInflater.from().inflate()方法得到一个View,再设置view里面的控件的值,这些控件是将要显示在ListView每个item中的控件,所以要新建一个list_cell.layout文件,里面放置四个TexView用来显示四个字段:id,username,age,sex
然后我们就可以来写getView方法了
好了,到这里完成了自定义ArrayAdapter的设置(很简单)
完整的UserArrayAdapter代码:
然后在MainActivity中新建一个UserArrayAdapter
首先传回的JSON数据格式为:
{ "userinfo":[{"username":"zhangsan","id":17,"age":12,"sex":"nan"}, {"username":"zhangsan","id":18,"age":12,"sex":"nan"},...]}
很清楚一个键为”userinfo“值为JSON数组的对象
JSON数组里的对象包含四对Name-Value
所以我们的对象是一个User包含上面的四个字段:uername,id,age,sex;
User.java还有一个静态方法用来将JSON字符串解析为User对象数组
package com.sky.firsthttpapp; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; /** * Created by Administrator on 2015/11/20. */ public class User { private int id; private String username; private String sex; private int age; public User(int id, String username, String sex, int age) { this.id = id; this.username = username; this.sex = sex; this.age = age; } public int getId() { return id; } public String getUsername() { return username; } public String getSex() { return sex; } public int getAge() { return age; } public static ArrayList<User> getUserListFromJSONStrig(String JSONString){ ArrayList list=new ArrayList<User>(); try { JSONObject root=new JSONObject(JSONString); JSONArray array=root.getJSONArray("userinfo"); for(int i=0;i<array.length();i++){ JSONObject item= (JSONObject) array.get(i); User user=new User(item.getInt("id"), item.getString("username"), item.getString("sex"), item.getInt("age")); list.add(user); } } catch (JSONException e) { MessageManager.sendMessage("exception","JSONException:"+e.getMessage()); e.printStackTrace(); } return list; } }
通过User 的静态方法 getUserArrayFromJSONString()可以得到一个List。
下面通过自定义ArrayAdapter显示List的数据
新建一个类UserArrayAdapter,继承自ArrayAdapter,(超类没有无参构造函数)必须为其显示添加一个构造方法调用超类的构造方法
这里采用和超类一样的三个基本参数,Context context, int resource, List objects
接下来是最关键的一步,就是重写基类的getView方法,让他返回一个自定义的ListView Item的View,
首先通过LayoutInflater.from().inflate()方法得到一个View,再设置view里面的控件的值,这些控件是将要显示在ListView每个item中的控件,所以要新建一个list_cell.layout文件,里面放置四个TexView用来显示四个字段:id,username,age,sex
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvId" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <TextView android:id="@+id/tvName" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <TextView android:id="@+id/tvAge" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <TextView android:id="@+id/tvSex" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="fill_parent" /> </LinearLayout>
然后我们就可以来写getView方法了
@Override public View getView(int position, View convertView, ViewGroup parent) { //得到view View v= LayoutInflater.from(super.getContext()).inflate(R.layout.list_cell,null,false); //取得控件 TextView tvId= (TextView) v.findViewById(R.id.tvId); TextView tvName= (TextView) v.findViewById(R.id.tvName); TextView tvAge= (TextView) v.findViewById(R.id.tvAge); TextView tvSex= (TextView) v.findViewById(R.id.tvSex); //根据position取得user对象,并设置控件的text User user=super.getItem(position); tvId.setText(user.getId()+""); tvName.setText(user.getUsername()); tvAge.setText(user.getAge()+""); tvSex.setText(user.getSex()); return v; }
好了,到这里完成了自定义ArrayAdapter的设置(很简单)
完整的UserArrayAdapter代码:
public class UserArrayAdapter extends ArrayAdapter<User>{ public UserArrayAdapter(Context context, int resource, List<User> objects) { super(context, resource, objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v= LayoutInflater.from(super.getContext()).inflate(R.layout.list_cell,null,false); TextView tvId= (TextView) v.findViewById(R.id.tvId); TextView tvName= (TextView) v.findViewById(R.id.tvName); TextView tvAge= (TextView) v.findViewById(R.id.tvAge); TextView tvSex= (TextView) v.findViewById(R.id.tvSex); User user=super.getItem(position); tvId.setText(user.getId()+""); tvName.setText(user.getUsername()); tvAge.setText(user.getAge()+""); tvSex.setText(user.getSex()); return v; } }
然后在MainActivity中新建一个UserArrayAdapter
ArrayList<User> list=User.getUserListFromJSONString(result); adapter=new UserArrayAdapter(this,R.layout.list,list); lv.setAdapter(adapter);
相关文章推荐
- Android 中运用单例模式的方法说明
- 转:android studio 改编译区背景色
- 【Android】下拉刷新实现
- Android系统源码极速搜索引擎(OpenGrok)
- android开发技术-小白篇(一)
- 【Glide】使用
- 【广播】两个Activity进行广播通讯
- 【设置】沉浸式状态栏、导航栏
- Android优秀开发资源
- 百度地图系列01——最简单的调用百度地图
- Android 事件分发机制详解
- java自带线程池和队列详细讲解,android中适用
- Android性能优化典范
- android加载速度优化,通过项目的优化过程分析
- Android应用setContentView与LayoutInflater加载解析机制源码分析
- 搭建Android环境遇到问题的解决方案
- Android性能之内存基础篇(二)
- 路由器 DNSMasq 替代 hosts,支持Android、iPhone、PC
- android获取在res文件下的图片资源
- Android 图片三级缓存