@Override public Boolean workInBackground(boolean[]... params) throws TaskException { final boolean[] checkedItems = params[0]; final boolean[] editCheckedItems = params[1]; mGroupMemberListed = new GroupMemberListed(); mGroupMemberListed.setLists(new ArrayList<GroupListed>()); for (int i = 0; i < editCheckedItems.length; i++) { Group group = AppContext.getGroups().getLists().get(i); GroupListed groupListed = new GroupListed(); groupListed.setIdstr(group.getIdstr()); groupListed.setName(group.getName()); // 这个分组没有选中 if (editCheckedItems[i]) { // 如果这个分组原来也是选中的,则不需要编辑 if (checkedItems[i]) { mGroupMemberListed.getLists().add(groupListed); } // 如果这个分组原来没有选中,则添加 else { // 添加好友到分组 SinaSDK.getInstance(AppContext.getToken()) .friendshipsGroupdMembersAdd(mUser.getIdstr(), group.getIdstr()); publishProgress( String.format(getString(R.string.profile_add_to_group), group.getName())); mGroupMemberListed.getLists().add(groupListed); } } else { // 如果原来有这个分组,现在没有勾选,则删除 if (checkedItems[i]) { // 从分组中删除好友 SinaSDK.getInstance(AppContext.getToken()) .friendshipsGroupdMembersDestory(mUser.getIdstr(), group.getIdstr()); publishProgress( String.format(getString(R.string.profile_group_remvoe_friend), group.getName())); } // 原来没有勾选,现在也没有勾选,不做处理 } } return true; }
private void setGroupList() { if (groupDialog != null && groupDialog.isShowing()) groupDialog.dismiss(); String[] items = new String[AppContext.getGroups().getLists().size()]; final boolean[] checkedItems = new boolean[AppContext.getGroups().getLists().size()]; final boolean[] editCheckedItems = new boolean[AppContext.getGroups().getLists().size()]; for (int i = 0; i < AppContext.getGroups().getLists().size(); i++) { Group group = AppContext.getGroups().getLists().get(i); items[i] = group.getName(); checkedItems[i] = false; editCheckedItems[i] = false; for (GroupListed groupListed : mGroupMemberListed.getLists()) { if (groupListed.getIdstr().equals(group.getIdstr())) { checkedItems[i] = true; editCheckedItems[i] = true; break; } } } View customTitle = View.inflate(getActivity(), R.layout.lay_group_dialogtitle, null); customTitle .findViewById(R.id.btnSettings) .setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // 设置分组 GroupSortFragment.lanuch(getActivity()); } }); groupDialog = new AlertDialog.Builder(getActivity()) .setCustomTitle(customTitle) .setMultiChoiceItems( items, editCheckedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { editCheckedItems[which] = isChecked; } }) .setOnDismissListener( new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { groupDialog = null; } }) .setNegativeButton(R.string.cancel, null) .setPositiveButton( R.string.confirm, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { new SetGroupTask().execute(checkedItems, editCheckedItems); } }) .show(); }