Json解析和可扩展的ListView

本文是解析加载本地的Json,为了在不搭建服务器的情况下便于测试。。。

user.json

{    "result": "1",
    "message": "organ list",
    "groups": [
        {
            "groupid": 1,
            "groupname": "开发组",
            "users": [
                {
                    "userid": 2,
                    "name": "王老师",
                    "clientid": 100005,
                    "power": "57",
                    "gpsstatus": "1",
                    "clientdate": "2011-11-08 16:30:23",
                    "blogcount": 0
                },
                {
                    "userid": 226,
                    "name": "李岩",
                    "clientid": 100202,
                    "power": "54",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-24 14:00:58",
                    "blogcount": 0
                },
                {
                    "userid": 6,
                    "name": "DCC",
                    "clientid": 100001,
                    "power": "78",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-29 16:00:39",
                    "blogcount": 0
                },
                {
                    "userid": 225,
                    "name": "刘学佳",
                    "clientid": 100203,
                    "power": "98",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-16 19:01:41",
                    "blogcount": 0
                },
                {
                    "userid": 1,
                    "name": "David",
                    "clientid": 100000,
                    "power": "50",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-29 16:30:39",
                    "blogcount": 0
                },
                {
                    "userid": 405,
                    "name": "平台查看位置信息专用",
                    "clientid": 0,
                    "power": "0",
                    "gpsstatus": "0",
                    "clientdate": "现在",
                    "blogcount": 0
                }
            ]
        },
        {
            "groupid": 2,
            "groupname": "测试组",
            "users": [
                {
                    "userid": 194,
                    "name": "张忠文测试",
                    "clientid": 100176,
                    "power": "49",
                    "gpsstatus": "1",
                    "clientdate": "2011-10-26 13:00:10",
                    "blogcount": 0
                },
                {
                    "userid": 197,
                    "name": "郭磊测试",
                    "clientid": 100179,
                    "power": "0",
                    "gpsstatus": "0",
                    "clientdate": "现在",
                    "blogcount": 0
                },
                {
                    "userid": 136,
                    "name": "杜鹃测试",
                    "clientid": 100121,
                    "power": "50",
                    "gpsstatus": "1",
                    "clientdate": "2011-09-20 15:30:12",
                    "blogcount": 0
                },
                {
                    "userid": 232,
                    "name": "华为SONIC测试",
                    "clientid": 100207,
                    "power": "40",
                    "gpsstatus": "0",
                    "clientdate": "2012-02-14 15:35:51",
                    "blogcount": 0
                },
                {
                    "userid": 114,
                    "name": "中国联通V880",
                    "clientid": 100105,
                    "power": "2",
                    "gpsstatus": "0",
                    "clientdate": "2012-01-20 10:25:36",
                    "blogcount": 0
                },
                {
                    "userid": 118,
                    "name": "联想双卡双待",
                    "clientid": 100107,
                    "power": "77",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-27 23:00:23",
                    "blogcount": 0
                },
                {
                    "userid": 269,
                    "name": "山河N760测试",
                    "clientid": 100240,
                    "power": "22",
                    "gpsstatus": "1",
                    "clientdate": "2012-01-02 13:06:54",
                    "blogcount": 0
                },
                {
                    "userid": 88,
                    "name": "中国移动",
                    "clientid": 100082,
                    "power": "32",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-02 13:00:36",
                    "blogcount": 0
                },
                {
                    "userid": 288,
                    "name": "山河123",
                    "clientid": 100254,
                    "power": "86",
                    "gpsstatus": "1",
                    "clientdate": "2011-12-12 17:30:42",
                    "blogcount": 0
                },
                {
                    "userid": 369,
                    "name": "liyan",
                    "clientid": 0,
                    "power": "0",
                    "gpsstatus": "0",
                    "clientdate": "现在",
                    "blogcount": 0
                },
                {
                    "userid": 137,
                    "name": "张晨",
                    "clientid": 100174,
                    "power": "37",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-28 17:23:17",
                    "blogcount": 0
                }
            ]
        },
        {
            "groupid": 4,
            "groupname": "演示组",
            "users": [
                {
                    "userid": 12,
                    "name": "孙晗",
                    "clientid": 100017,
                    "power": "91",
                    "gpsstatus": "0",
                    "clientdate": "2012-02-29 10:40:50",
                    "blogcount": 0
                },
                {
                    "userid": 121,
                    "name": "华强普通测试",
                    "clientid": 0,
                    "power": "0",
                    "gpsstatus": "0",
                    "clientdate": "现在",
                    "blogcount": 0
                },
                {
                    "userid": 91,
                    "name": "体验账户",
                    "clientid": 0,
                    "power": "0",
                    "gpsstatus": "0",
                    "clientdate": "现在",
                    "blogcount": 0
                },
                {
                    "userid": 82,
                    "name": "郭磊",
                    "clientid": 100079,
                    "power": "56",
                    "gpsstatus": "0",
                    "clientdate": "2012-02-29 16:50:18",
                    "blogcount": 0
                },
                {
                    "userid": 15,
                    "name": "李丹丹",
                    "clientid": 100112,
                    "power": "90",
                    "gpsstatus": "1",
                    "clientdate": "2011-12-27 16:44:06",
                    "blogcount": 0
                },
                {
                    "userid": 78,
                    "name": "杜娟",
                    "clientid": 100075,
                    "power": "21",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-29 16:30:52",
                    "blogcount": 0
                },
                {
                    "userid": 238,
                    "name": "摩托525",
                    "clientid": 100212,
                    "power": "100",
                    "gpsstatus": "1",
                    "clientdate": "2011-11-23 14:00:36",
                    "blogcount": 0
                },
                {
                    "userid": 22,
                    "name": "张忠文",
                    "clientid": 100232,
                    "power": "33",
                    "gpsstatus": "0",
                    "clientdate": "2012-02-27 06:09:39",
                    "blogcount": 0
                },
                {
                    "userid": 196,
                    "name": "徐子鉴",
                    "clientid": 100178,
                    "power": "39",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-29 16:31:21",
                    "blogcount": 0
                },
                {
                    "userid": 88,
                    "name": "中国移动",
                    "clientid": 100082,
                    "power": "32",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-02 13:00:36",
                    "blogcount": 0
                },
                {
                    "userid": 243,
                    "name": "毛宇飞",
                    "clientid": 100215,
                    "power": "15",
                    "gpsstatus": "0",
                    "clientdate": "2012-02-29 16:50:14",
                    "blogcount": 0
                },
                {
                    "userid": 372,
                    "name": "6987",
                    "clientid": 100324,
                    "power": "5",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-27 16:00:34",
                    "blogcount": 0
                },
                {
                    "userid": 373,
                    "name": "6981",
                    "clientid": 100325,
                    "power": "92",
                    "gpsstatus": "1",
                    "clientdate": "2012-02-13 13:00:35",
                    "blogcount": 0
                }
            ]
        }
    ]
}


每个组的javaBean

Group.java

package com.example.json;

import java.util.List;

public class Group {
	private String groupId;
	private String groupName;
	private List<User> users;
	
	public Group() {
		super();
	}

	public Group(String groupId, String groupName, List<User> users) {
		super();
		this.groupId = groupId;
		this.groupName = groupName;
		this.users = users;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return "Group [groupId=" + groupId + ", groupName=" + groupName
				+ ", users=" + users + "]";
	}

	
}

展开后的数据的javaBean

User.java

package com.example.json;

public class User {
	private String userId;
	private String name;
	private String clientId;
	private String power;
	private String gpsStatus;
	private String clientDate;
	private String blogCount;
	
	private boolean isSelected = false;

	public User() {
		super();
	}

	public User(String userId, String name, String clientId, String power,
			String gpsStatus, String clientDate, String blogCount) {
		super();
		this.userId = userId;
		this.name = name;
		this.clientId = clientId;
		this.power = power;
		this.gpsStatus = gpsStatus;
		this.clientDate = clientDate;
		this.blogCount = blogCount;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getPower() {
		return power;
	}

	public void setPower(String power) {
		this.power = power;
	}

	public String getGpsStatus() {
		return gpsStatus;
	}

	public void setGpsStatus(String gpsStatus) {
		this.gpsStatus = gpsStatus;
	}

	public String getClientDate() {
		return clientDate;
	}

	public void setClientDate(String clientDate) {
		this.clientDate = clientDate;
	}

	public String getBlogCount() {
		return blogCount;
	}

	public void setBlogCount(String blogCount) {
		this.blogCount = blogCount;
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	@Override
	public String toString() {
		return "User [blogCount=" + blogCount + ", clientDate=" + clientDate
				+ ", clientId=" + clientId + ", gpsStatus=" + gpsStatus
				+ ", name=" + name + ", power=" + power + ", userId=" + userId
				+ "]";
	}

}

解析json

JsonParser.java

package com.example.json;

import java.io.InputStream;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class JsonParser {

	public static ArrayList<Group> getGroups(InputStream inputStream)throws Exception {

		ArrayList<Group> groups = new ArrayList<Group>();
		byte[] bts = StreamTool.readStream(inputStream);
		String contents = new String(bts, "gb2312");

		JSONArray groupArr = new JSONObject(contents).getJSONArray("groups");

		int groupsCount = groupArr.length();

		for (int i = 0; i < groupsCount; i++) {

			Group g = new Group();

			JSONObject group = (JSONObject) groupArr.opt(i);
			String groupId = group.getString("groupid");
			String groupName = group.getString("groupname");
			JSONArray usersArr = group.getJSONArray("users");

			int usersCount = usersArr.length();
			ArrayList<User> users = new ArrayList<User>();
			for (int j = 0; j < usersCount; j++) {

				User u = new User();

				JSONObject user = (JSONObject) usersArr.opt(j);
				String userId = user.getString("userid");
				String name = user.getString("name");
				String clientId = user.getString("clientid");
				String power = user.getString("power");
				String gpsStatus = user.getString("gpsstatus");
				String clientDate = user.getString("clientdate");
				String blogCount = user.getString("blogcount");

				Log.i("test", "User [blogCount=" + blogCount + ", clientDate="
						+ clientDate + ", clientId=" + clientId
						+ ", gpsStatus=" + gpsStatus + ", name=" + name
						+ ", power=" + power + ", userId=" + userId + "]");
				u.setUserId(userId);
				u.setName(name);
				u.setClientId(clientId);
				u.setPower(power);
				u.setGpsStatus(gpsStatus);
				u.setClientDate(clientDate);
				u.setBlogCount(blogCount);

				users.add(u);
			}

			g.setGroupId(groupId);
			g.setGroupName(groupName);
			g.setUsers(users);
			groups.add(g);
		}

		if (inputStream != null) {
			inputStream.close();
//			
		}
		return groups;

	}

//	public static ArrayList<Messages> getTrcak(InputStream inputStream)
//			throws Exception {
//		ArrayList<Messages> list = new ArrayList<Messages>();
//		byte[] bts = StreamTool.readStream(inputStream);
//		String contents = new String(bts, "gb2312");
//
//		JSONObject countObject = new JSONObject(contents);
//		String count = countObject.getString("count");
//		if (count.equals("0")){
//			
//			return null;
//		}
//			
//		JSONArray locationsArr = new JSONObject(contents).getJSONArray("locations");
//		int locationsCount = locationsArr.length();
//		for (int i = 0; i < locationsCount; i++) {
//			JSONObject locationObject = (JSONObject) locationsArr.opt(i);
//			JSONObject location = locationObject.getJSONObject("location");
//			Messages message = new Messages();
//			String longitnde = location.getString("clng");
//			String latitude = location.getString("clat");
//			String time = location.getString("serverdate");
//			String address = location.getString("locationinfo");
//
//			message.setAddress(address);
//			message.setLongitnde(longitnde);
//			message.setLatitude(latitude);
//			message.setTime(time);
//
//			list.add(message);
//		}
//		return list;
//
//	}

}


适配器GroupExpandablebaseAdapter

GroupExpandablebaseAdapter.java

package com.example.json;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class GroupExpandablebaseAdapter extends BaseExpandableListAdapter {

	private Context context;

	//准备一级列表中显示的数据
	List<Group> accountFathers = new ArrayList<Group>();
	// 准备二级列表中显示的数据
	List<List<User>> accountChlidrens = new ArrayList<List<User>>();


	public GroupExpandablebaseAdapter(Context context,
			List<Group> accountFathers) {
		super();
		this.context = context;
		this.accountFathers = accountFathers;
		for (int i = 0; i < accountFathers.size(); i++) {
			accountChlidrens.add(accountFathers.get(i).getUsers());
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return accountChlidrens.get(groupPosition).get(childPosition).getName().toString();
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = null;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.user_item, null);
		// user名字
		TextView txtBalance = (TextView) view.findViewById(R.id.txtUserName);
		txtBalance.setText(accountChlidrens.get(groupPosition).get(childPosition).getName().toString());
		final CheckBox isCheck = (CheckBox) view.findViewById(R.id.userCheck);

		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				if (!isChecked(groupPosition, childPosition)) {
//					isCheck.setChecked(true);
//					accountChlidrens.get(groupPosition).get(childPosition).setSelected(true);
//					Log.i("11111111111", accountChlidrens.get(groupPosition).get(childPosition)+"");
//				} else {
//					isCheck.setChecked(false);
//					accountChlidrens.get(groupPosition).get(childPosition).setSelected(false);
//					Log.i("22222222222", accountChlidrens.get(groupPosition).get(childPosition)+"");
//				}
				Log.i("11111111111", accountChlidrens.get(groupPosition).get(childPosition)+"");

			}
		});
		isCheck.setChecked(isChecked(groupPosition, childPosition));
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return accountChlidrens.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub

		return accountFathers.get(groupPosition).getGroupName().toString();
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return accountFathers.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.group_item, null);
		}
		// 一级列表中显示的标题
		TextView txtFather = (TextView) view.findViewById(R.id.txtFather);
		txtFather.setText(getGroup(groupPosition).toString());
		return view;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	// 是否被选中checkbox
	public boolean isChecked(int groupPosition, int childPosition) {
		return accountChlidrens.get(groupPosition).get(childPosition).isSelected();
	}

}

StreamTool.java

package com.example.json;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {

	/**
	 * 读取流
	 * 
	 * @param inStream
	 * @return 字节数组
	 * @throws Exception
	 */

	public static byte[] readStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = -1;
		while ((len = inStream.read(buffer)) != -1) {
			outSteam.write(buffer, 0, len);
		}
		outSteam.close();
		inStream.close();
		return outSteam.toByteArray();
	}
}

MainActivity.java

package com.example.json;

import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {

	ExpandableListView listView;
	GroupExpandablebaseAdapter adapter;
	
	List<Group> groups;
//	List<Messages> messages;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		InputStream inputStream = getBaseContext().getClassLoader().getResourceAsStream("user.json");
		try {
        	groups = JsonParser.getGroups(inputStream);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		listView = (ExpandableListView) findViewById(R.id.expandableListView);
		adapter = new GroupExpandablebaseAdapter(this, groups);
		listView.setAdapter(adapter);
		Drawable drawable = this.getResources().getDrawable(R.drawable.dm);
		// 去掉系统自带的按钮
		listView.setGroupIndicator(drawable);
		// 去掉系统自带的分隔线
		listView.setDivider(null);
		
		
	}
	
}


代码已经上传,等审核通过了,就黏贴下载地址。。。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值