Get contact name from just the number
- by user1190019
Say i have an edit text and a button. In the edit text you would type a number and then when you hit the button it will either show the contact information or return with the name of that contact.
I have tried all sorts of methods provided with no luck. The one i have successfully gotten the furthest with was the following... But i had no luck returning the name.
Cursor phoneCursor = null;
contactList = new HashMap<String,String>();
try{
Uri uContactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String strProjection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
phoneCursor = getContentResolver().query(uContactsUri, null, null, null, strProjection);
phoneCursor.moveToFirst();
String name = "";
String phoneNumber = "";
int nameColumn = phoneCursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneColumn = phoneCursor.getColumnIndex(Phone.NUMBER);
phoneCursor.moveToNext();
}
}
catch(Exception e){
Log.e("[SmsMain] getContactData", e.toString());
}
finally{
if(phoneCursor != null){
phoneCursor.close();
phoneCursor = null;
}
}
}