@Override protected void setUpView() { titleBar.setRightImageResource(com.xuhai.telescopes.R.drawable.em_add); titleBar.setRightLayoutClickListener( new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(), NewAndCreateActivity.class)); } }); // 设置联系人数据 setContactsMap(MyHelper.getInstance().getContactList()); super.setUpView(); listView.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String username = ((EaseUser) listView.getItemAtPosition(position)).getUsername(); // demo中直接进入聊天页面,实际一般是进入用户详情页 startActivity( new Intent(getActivity(), ChatActivity.class).putExtra("userId", username)); } }); // 进入添加好友页 titleBar .getRightLayout() .setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(), AddContactActivity.class)); } }); contactSyncListener = new ContactSyncListener(); MyHelper.getInstance().addSyncContactListener(contactSyncListener); blackListSyncListener = new BlackListSyncListener(); MyHelper.getInstance().addSyncBlackListListener(blackListSyncListener); contactInfoSyncListener = new ContactInfoSyncListener(); MyHelper.getInstance() .getUserProfileManager() .addSyncContactInfoListener(contactInfoSyncListener); if (!MyHelper.getInstance().isContactsSyncedWithServer()) { loadingView.setVisibility(View.VISIBLE); } else { loadingView.setVisibility(View.GONE); } }
@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(); } } }
@Override public void onDestroy() { super.onDestroy(); if (contactSyncListener != null) { MyHelper.getInstance().removeSyncContactListener(contactSyncListener); contactSyncListener = null; } if (blackListSyncListener != null) { MyHelper.getInstance().removeSyncBlackListListener(blackListSyncListener); } if (contactInfoSyncListener != null) { MyHelper.getInstance() .getUserProfileManager() .removeSyncContactInfoListener(contactInfoSyncListener); } }
@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(); } }); }