I'm having a problem with extracting phone numbers of some people in my contact list.
First I show all the contacts in a listview:
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
mCursor = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] {mContactId}, null);
When clicking on an item, this is how I fetch the contact_id:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Cursor currentCursor = mContactsAdapter.getCursor();
if (currentCursor != null) {
notifyOnContactSelectedListeners(String.valueOf(id));
}
}
Then I create a new fragment, and while loading it I query for the contact's phone & display name:
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
So for some people that has a phone, I get the phone number this way and that's ok. But for some people I can't get the phone number this way - but they do have phone number in the default's phone contacts book.
What went wrong?
I had a similar difficulty. I discovered that the numbers that I was unable to receive had all been imported from my linked Facebook account. You will be able to detect that the contact exists, and indeed that they have a phone number. However, when you try to retrieve said number with a SQL query the result returned will be
null
.It transpired that Facebook restrict access to their contacts for security reasons. I am yet to find another provider (e.g. LinkedIn, Google) which hides phone numbers.
Further reading: Cannot find Facebook contacts in RawContacts
I received
null
in some contacts.I verified my code to find out that when querying phone numbers I was using
ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER
then I changed toContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER
that according to android docs it says it isand the code worked well.
try this may it useful for you
public class Contact extends Activity implements OnItemClickListener{
}