Retrieval Of Firstname And Lastname From Android Contacts Results In '1' And 'null'
I am retrieving firstname and lastname from the android contact using the below code.DISPLAY_NAME gives back the name of the contact while firstname and lastname returns 1 and null
Solution 1:
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();
Post a Comment for "Retrieval Of Firstname And Lastname From Android Contacts Results In '1' And 'null'"