Android - find contact by display name

I am trying to find a contact by display name. The goal is to open this contact and add more data to it (in particular, more phone numbers), but I'm struggling to find the contact I want to update.

This is the code I'm using:

public static String findContact(Context context) { ContentResolver contentResolver = context.getContentResolver(); Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI; String[] projection = new String[] { PhoneLookup._ID }; String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ?"; String[] selectionArguments = { "John Johnson" }; Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null); if (cursor != null) { while (cursor.moveToNext()) { return cursor.getString(0); } } return "John Johnson not found"; } 

I have a contact called "John Johnson", but the method always returns "not found." I also tried to find a contact with only one name, so it does not matter.

I suspect that something is wrong with the uri, select or select arguments, because I could not find a single example of online searching for contacts with a given display name, and it seems that the display name is a special kind of information, for example, a phone number .

Any ideas how I can get to find John Johnson?


UPDATE: I found out how to find a contact by display name:

  ContentResolver contentResolver = context.getContentResolver(); Uri uri = Data.CONTENT_URI; String[] projection = new String[] { PhoneLookup._ID }; String selection = StructuredName.DISPLAY_NAME + " = ?"; String[] selectionArguments = { "John Johnson" }; Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null); if (cursor != null) { while (cursor.moveToNext()) { return cursor.getString(0); } } return "John Johnson not found"; 

This code returns the contact identifier of the first contact with the display name "John Johnson". In my source code, I had the wrong uri and the wrong choice in my request.

+8
android android-contentresolver
source share
3 answers

I think the problem may be caused by the projection you created. Projection is used to indicate android, in the data column that you want to query, then you only specify the id column so that the display name is not returned. Try deleting the projection to see if it works.

-- Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);
++ Cursor cursor = contentResolver.query(uri, null, selection, selectionArguments, null);

+1
source share
  //method for gaining id //this method get a name and make fetch it id and then send the id to other method //named "showinformation" and that method print information of that contact public void id_return(String name) { String id_name=null; Uri resultUri = ContactsContract.Contacts.CONTENT_URI; Cursor cont = getContentResolver().query(resultUri, null, null, null, null); String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " = ?" ; String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,name}; Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); while (nameCur.moveToNext()) { id_name = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID));} nameCur.close(); cont.close(); nameCur.close(); //for calling of following method showinformation(id_name); } //method for showing information like name ,phone, email and other thing you want public void showinformation(String id) { String name=null; String phone=null; String email=null; Uri resultUri = ContactsContract.Contacts.CONTENT_URI; Cursor cont = getContentResolver().query(resultUri, null, null, null, null); String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID+ " = ?" ; String[] whereNameParams1 = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,id}; Cursor nameCur1 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams1, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); while (nameCur1.moveToNext()) { name = nameCur1.getString(nameCur1.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));} nameCur1.close(); cont.close(); nameCur1.close(); String[] whereNameParams2 = new String[] { ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,id}; Cursor nameCur2 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams2, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); while (nameCur2.moveToNext()) { phone = nameCur2.getString(nameCur2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));} nameCur2.close(); cont.close(); nameCur2.close(); String[] whereNameParams3 = new String[] { ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,id}; Cursor nameCur3 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams3, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); while (nameCur3.moveToNext()) { email = nameCur3.getString(nameCur3.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));} nameCur3.close(); cont.close(); nameCur3.close(); String[] whereNameParams4 = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,id}; Cursor nameCur4 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams4, ContactsContract.CommonDataKinds.StructuredPostal.DATA); while (nameCur4.moveToNext()) { phone = nameCur4.getString(nameCur4.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA));} nameCur4.close(); cont.close(); nameCur4.close(); //showing result txadd.setText("Name= "+ name+"\nPhone= "+phone+"\nEmail= "+email); } //thank all persons in this site because of many help of me to learn and correction my warn and errors this is only a gift for all of you and ... 
0
source share

The code below should do the trick

  if (displayName != null) { Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(displayName)); String[] displayNameProjection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME }; Cursor cur = context.getContentResolver().query(lookupUri, displayNameProjection, null, null, null); try { if (cur.moveToFirst()) { return true; } } finally { if (cur != null) cur.close(); } return false; } else { return false; } 

Link: Retrieving a List of Articles for Contacts

0
source share

All Articles