本文是解析加載本地的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);
}
}
代碼已經上傳,等稽核通過了,就黏貼下載下傳位址。。。