Retrieving a phone number with ContactsContract in Android - function doesn't work

后端 未结 3 1953
渐次进展
渐次进展 2021-01-01 04:08

I wrote the following function in order to retrieve one single phone number that belongs to the contact with id \"contactID\".

The function which is to retri

3条回答
  •  梦毁少年i
    2021-01-01 04:27

    You are getting null because you have set your projection to null. The projection is basically the list of columns that you want returned e.g.

    String[] projection = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};
    

    Usually, when you find the contact, they may have a list of phone numbers, so you have to use another cursor to iterate through the phone numbers, e.g.

    Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);    
    while (phones.moveToNext()) 
    {    
         phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
    }
    

    Hope this helps.

提交回复
热议问题