Retrieval of firstname and lastname from android contacts results in '1' and 'null'

☆樱花仙子☆ 提交于 2019-12-03 03:47:10

The following code will help you get first name and last name:

Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri dataUri = Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY);
Cursor nameCursor = getActivity().getContentResolver().query(
        dataUri,
        null,
        Data.MIMETYPE+"=?",
        new String[]{ StructuredName.CONTENT_ITEM_TYPE },
        null);
           while (nameCursor.moveToNext())
            {

                String      firstName = nameCursor.getString(nameCursor.getColumnIndex(Data.DATA2));
                String  lastName = nameCursor.getString(nameCursor.getColumnIndex(Data.DATA3));

                Toast.makeText(getApplicationContext(), "First name"+firstName, Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "Second name"+lastName, Toast.LENGTH_LONG).show();

                return new String [] {firstName , lastName};
            }

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