您的位置:首页 > 移动开发 > Android开发

Android——自定义ArrayAdapter

2015-11-20 20:44 405 查看
在取得网页端传回的JSON数据并封装成对象数据后,想要把对象数据的信息显示在一个ListView中,在这里使用自定义ArrayAdapter给ListView添加adapter。

首先传回的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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: