Question

I am trying to create a Custom Contact app which displays only those contacts that have Contact Number. First of all, is there any automated way to do it? Suppose not, then I am trying to search a contact by its name e.g. Rohan.

Here is the code :-

Cursor photoCursor = getContentResolver().query(
            android.provider.ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.Contacts.DISPLAY_NAME },
            ContactsContract.Contacts.DISPLAY_NAME + " = ?",
            new String[]{"Rohan"}, null);
    photoCursor.moveToFirst();
    while (photoCursor.moveToNext()) {
        Log.d("Photo Thumbnail", "" + photoCursor.getString(1));
    }

Although the contact exists, I am not getting any Log, if I remove Selection & Selection Args I see Rohan in the log. What am I doing wrong?

Was it helpful?

Solution 2

I did it by using the following code

Cursor cursor = getContentResolver().query(
            android.provider.ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts._ID },
            ContactsContract.Contacts.HAS_PHONE_NUMBER, null,
            ContactsContract.Contacts.DISPLAY_NAME);

This cursor gives all the contacts that have any phone number and then i save the unique ID in an ArrayList like this

cursor.moveToFirst();

    while (cursor.moveToNext()) {
        contactsID.add(cursor.getString(2));
    }

then on selecting the contact i find the contact numbers using this

Cursor cursor = getContentResolver()
                    .query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            new String[] {
                                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?",
                            new String[] { contactsID.get(position) }, null);
            contactNumbers = new ArrayList<String>();
            while (cursor.moveToNext()) {
                contactNumbers.add(cursor.getString(0));
                Log.d("number", cursor.getString(0));
            }

OTHER TIPS

Simple Solution for Searching Partial Display Name.

ContentResolver contentResolver = getCurrentActivity().getContentResolver();

String   whereString = "display_name LIKE ?";
String[] whereParams = new String[]{ "%" + searchText + "%" };

Cursor contactCursor = contentResolver.query(
        ContactsContract.Data.CONTENT_URI,
        null,
        whereString,
        whereParams,
        null );

while( contactCursor.moveToNext() ) {

    int contactId = getIntFromCursor( contactCursor, ContactsContract.Data.CONTACT_ID );

    Log.d( "Contact ID", contactId)

}

contactCursor.close();

Try this:

    Cursor contactLookupCursor =
            getContentResolver().query(
                    Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                            Uri.encode("Rohan")),
                    new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER},
                    null,
                    null,
                    null);

    try {
        while (contactLookupCursor.moveToNext()) {
            contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
            contactNumber = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.NUMBER));
        }
    } finally {
        contactLookupCursor.close();
    }

It looks like you are trying to implement a screen that will allow the user to select a contact, and then select a phone number of that contact.

If that's the case, you can use a phone-picker intent instead:

Intent intent = Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);

This will open the native Contacts app, and allow the user to select a contact, and a phone number. You'll then receive the result in your app like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
        // Get the URI and query the content provider for the phone number
        Uri contactUri = data.getData();
        String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(contactUri, projection,
                null, null, null);
        // If the cursor returned is valid, get the phone number
        if (cursor != null && cursor.moveToFirst()) {
            int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(numberIndex);
            // Do something with the phone number
            ...
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top