protected void chooseFromMultipleItems( List<ContactResult.ResultItem> items, CheckBox checkbox, String id) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); ArrayList<String> itemLabels = new ArrayList<String>(items.size()); for (ResultItem resultItem : items) { itemLabels.add(resultItem.getResult()); } class ClickListener implements OnCancelListener, OnClickListener, OnMultiChoiceClickListener { private List<ContactResult.ResultItem> items; private CheckBox checkbox; private String id; private boolean[] checked; public ClickListener(List<ContactResult.ResultItem> items, CheckBox checkbox, String id) { this.items = items; this.checkbox = checkbox; this.id = id; checked = new boolean[items.size()]; } @Override public void onClick(DialogInterface dialog, int which) { finish(); } @Override public void onClick(DialogInterface arg0, int which, boolean isChecked) { checked[which] = isChecked; } private void finish() { ArrayList<ContactResult.ResultItem> result = new ArrayList<ContactResult.ResultItem>(items.size()); for (int i = 0; i < items.size(); ++i) { if (checked[i]) { result.add(items.get(i)); } } if (result.size() == 0) { checkbox.setChecked(false); } else { results.put(id, new ContactResult(id, result)); } } @Override public void onCancel(DialogInterface dialog) { finish(); } } ClickListener clickListener = new ClickListener(items, checkbox, id); builder .setMultiChoiceItems(itemLabels.toArray(new String[0]), null, clickListener) .setOnCancelListener(clickListener) .setPositiveButton(android.R.string.ok, clickListener) .show(); }
@Override public void onItemClick(AdapterView<?> adapterView, View view, int pos, long rowId) { CheckBox checkbox = (CheckBox) view.findViewById(R.id.contactCheck); Cursor cursor = mCursorAdapter.getCursor(); cursor.moveToPosition(pos); String id = cursor.getString(0); if (checkbox.isChecked()) { checkbox.setChecked(false); results.remove(id); } else { checkbox.setChecked(true); Cursor itemCursor = getActivity() .getContentResolver() .query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {id}, null); List<ContactResult.ResultItem> resultItems = new LinkedList<ContactResult.ResultItem>(); itemCursorLoop: while (itemCursor.moveToNext()) { String contactNumber = itemCursor.getString( itemCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); String contactName = itemCursor.getString( itemCursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.DISPLAY_NAME)); String avatarURI = itemCursor.getString( itemCursor.getColumnIndex( ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI)); int contactKind = itemCursor.getInt( itemCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); for (ResultItem previousItem : resultItems) { if (contactNumber.equals(previousItem.getResult())) { continue itemCursorLoop; } } resultItems.add( new ContactResult.ResultItem(contactNumber, contactName, avatarURI, contactKind)); } itemCursor.close(); if (resultItems.size() > 1) { // contact has multiple items - user needs to choose from them chooseFromMultipleItems(resultItems, checkbox, id); } else { // only one result or all items are similar for this contact results.put(id, new ContactResult(id, resultItems)); } } }