private void showDialogOfSuggestedContacts(final Vector<Contact> vector) { if (vector != null) { // create content for alert ArrayList<String> array = new ArrayList<String>(); Iterator<Contact> it = vector.iterator(); while (it.hasNext()) { Contact contact = it.next(); array.add(contact.getDisplayName() + "\n" + contact.getPhoneNumber()); } AlertDialog.Builder alertContacts = new AlertDialog.Builder(this); alertContacts.setTitle("Suggessted Contacts : Pick One"); alertContacts.setSingleChoiceItems( array.toArray(new String[] {}), -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); Contact tempContact = vector.get(which); showMessage("Calling " + tempContact.getDisplayName()); callPhone(tempContact.getPhoneNumber()); } }); alertContacts.create(); alertContacts.show(); } }
private void showSuggestedContacts(ArrayList<String> nameList) { if (nameList != null) { Vector<Contact> setOfSuggestions = null; Iterator<String> it = nameList.iterator(); while (it.hasNext()) { String name = it.next(); // query contacts Cursor c = getContentResolver() .query( android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE '%" + name + "%'", null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); while (c.moveToNext()) { // initialize set if null if (setOfSuggestions == null) setOfSuggestions = new Vector<Contact>(); // obtaining details String displayName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Log.v("Name", displayName + " - " + phoneNumber); // create new contact Contact contact = new Contact(); contact.setDisplayName(displayName); contact.setPhoneNumber(phoneNumber); setOfSuggestions.add(contact); } c.close(); } // if no match found if (setOfSuggestions == null) { showMessage(Constants.CONTACT_NOT_FOUND); } else { // match found // check whether there is only one match if (setOfSuggestions.size() == 1) { Contact tempContact = setOfSuggestions.get(0); showMessage("Calling " + tempContact.getDisplayName()); callPhone(tempContact.getPhoneNumber()); } else { // promt user to pick one showDialogOfSuggestedContacts(setOfSuggestions); } } } }