Android - Find a contact by display name

拜拜、爱过 提交于 2019-12-05 01:41:50

Change Your Query URI.

You are using a URI that is meant to filter only phones numbers:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI;

You need to use a URI that has access to the display_name column, like this:

Uri uri = ContactsContract.Data.CONTENT_URI;

There's a decent breakdown of what URIs to use and when to use them on the Android SDK Documentation:

  • If you need to read an individual contact, consider using CONTENT_LOOKUP_URI instead of CONTENT_URI.

  • If you need to look up a contact by the phone number, use PhoneLookup.CONTENT_FILTER_URI, which is optimized for this purpose.

  • If you need to look up a contact by partial name, e.g. to produce filter-as-you-type suggestions, use the CONTENT_FILTER_URI URI.

  • If you need to look up a contact by some data element like email address, nickname, etc, use a query against the ContactsContract.Data table. The result will contain contact ID, name etc.

I thinks the issue may caused by the projection you set. Projection is used to tell android which column of data you want to query then you only give the id column so the display name won't return. Try to remove the projection to see whether it works.

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

           //method for gaining id
//this method get  a name  and make fetch it's 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 ...

The below code 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;
    }

Reference: Retrieving a List of Contacts Article

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!