@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(com.xuhai.telescopes.R.layout.em_activity_group_pick_contacts); if (exitingMembers == null) exitingMembers = new ArrayList<String>(); // 获取好友列表 final List<EaseUser> alluserList = new ArrayList<EaseUser>(); for (EaseUser user : MyHelper.getInstance().getContactList().values()) { if (!user.getUsername().equals(Constant.NEW_FRIENDS_USERNAME) & !user.getUsername().equals(Constant.GROUP_USERNAME) & !user.getUsername().equals(Constant.CHAT_ROOM) & !user.getUsername().equals(Constant.CHAT_ROBOT)) alluserList.add(user); } // 对list进行排序 Collections.sort( alluserList, new Comparator<EaseUser>() { @Override public int compare(EaseUser lhs, EaseUser rhs) { if (lhs.getInitialLetter().equals(rhs.getInitialLetter())) { return lhs.getNick().compareTo(rhs.getNick()); } else { if ("#".equals(lhs.getInitialLetter())) { return 1; } else if ("#".equals(rhs.getInitialLetter())) { return -1; } return lhs.getInitialLetter().compareTo(rhs.getInitialLetter()); } } }); listView = (ListView) findViewById(com.xuhai.telescopes.R.id.list); contactAdapter = new PickContactAdapter( this, com.xuhai.telescopes.R.layout.em_row_contact_with_checkbox, alluserList); listView.setAdapter(contactAdapter); ((EaseSidebar) findViewById(com.xuhai.telescopes.R.id.sidebar)).setListView(listView); listView.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { CheckBox checkBox = (CheckBox) view.findViewById(com.xuhai.telescopes.R.id.checkbox); checkBox.toggle(); } }); }
@Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { super.onSuccess(statusCode, headers, response); List<String> team = new ArrayList<>(); Map<String, List<String>> teamUsers = new HashMap(); if (statusCode == 200) { try { JSONArray allTeam = response.getJSONArray("groups"); Map<String, EaseUser> allUsers = new HashMap<String, EaseUser>(); // 所有用户 for (int i = 0; i < allTeam.length(); i++) { // 获得分组名 String teamName = allTeam.getJSONObject(i).getString("name"); Log.e("FromServer获得分组", teamName); team.add(teamName); // 获得这个分组的所有用户User JSONArray users = allTeam.getJSONObject(i).getJSONArray("users"); Log.e("这个分组用户", users.toString()); List<String> oneLineUsersNameList = new ArrayList<>(); // 这个分组的所有用户名 for (int j = 0; j < users.length(); j++) { JSONObject userjson = users.getJSONObject(j); Log.e("FromServer获得用户", userjson.toString()); oneLineUsersNameList.add(userjson.getString("name")); EaseUser user = null; user = setUserFromJson(userjson); allUsers.put(user.getUsername(), user); Log.e("allUser", allUsers.toString()); } teamUsers.put(teamName, oneLineUsersNameList); } MyHelper.getInstance().setTeam(team); MyHelper.getInstance().setTeamUsers(teamUsers); // 把所有用户存到内存 // 存入内存 MyHelper.getInstance().getContactList().clear(); MyHelper.getInstance().getContactList().putAll(allUsers); // 存入db UserDao dao = new UserDao(MyApplication.applicationContext); List<EaseUser> users = new ArrayList<EaseUser>(allUsers.values()); dao.saveContactList(users); } catch (JSONException e) { e.printStackTrace(); Log.e("FromServer", "解析分组信息出错"); } catch (Exception en) { en.printStackTrace(); } } }