由于最近后台返回数据只在一层的jsonObject 里面 所以好多数据需要自己去分类整理,很麻烦。现在将本人对分类的代码简单记录下 希望有用。
resParent.getCommon().getList();这个是服务器请求下来的数据
新建ArrayList集合list
list.addAll(resParent.getCommon().getList());ListphoneList = new ArrayList();
循环遍历list
for (int i = 0; i < list.size(); i++) {
得到集合中需要分类的每个字段
String phoneer = list.get(i).phoneer;
String phoneed = list.get(i).phoneed;
// 查看新集合phoneList中是否有指定的元素,如果没有则加入
if (!phoneList.contains(phoneed)) {
phoneList.add(phoneed);
}
if (!phoneList.contains(phoneer)) {
phoneList.add(phoneer);
}
}
//再遍历新集合phoneList 对其按照指定的元素分类
for (int i = 0; i < phoneList.size(); i++) {
String phone = phoneList.get(i);
addGroupList(phone);
}
private void addGroupList(String phone) {
List<?>childList = new ArrayList<>();
// TODO Auto-generated method stub
for (int j = 0; j < list.size(); j++) {
String phones = list.get(j).phoneed;
String phoness = list.get(j).phoneer;
if (phone.equals(phones) || phone.equals(phoness)) {
childList.add(new ChildEntity(list.get(j).aid,
list.get(j).orderid, list.get(j).type,
list.get(j).phoneer, list.get(j).caller,
list.get(j).called, list.get(j).phoneed,
list.get(j).starttime, list.get(j).endtime,
list.get(j).timelen, list.get(j).subtype,
list.get(j).callSid, list.get(j).recordurl,
list.get(j).byetype));
}
}
groupList.add(new Bean(childList));
}