Android get phone number from contact list

前端 未结 4 733
耶瑟儿~
耶瑟儿~ 2020-12-21 10:29

I have these codes which basically use a ListView to display the names in the contact list and I want to get their phone number when click each single name:

         


        
4条回答
  •  误落风尘
    2020-12-21 10:58

    it returns -1 because you don't request the column ContactsContract.CommonDataKinds.Phone.NUMBER from DB:

    new String[] {ContactsContract.Contacts.DISPLAY_NAME}
    

    ContactsContract.Contacts.DISPLAY_NAME is the only field you request.

    To be able to get the phone number, you firstly need to include it into list of columns you want to get from DB:

    new String[] {ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}

    Now you have to override adapter's getView so it sets the name into the textView of the list row. After that your onItemClick will work as expected

    | improve this answer | |

提交回复
热议问题