Android get phone number from contact list

前端 未结 4 704
耶瑟儿~
耶瑟儿~ 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 11:13

    The ContactsContract Android API stores data about contacts like phone number in the Data table, not the Contacts table.

    Read this carefully: https://developer.android.com/reference/android/provider/ContactsContract.html.

    Update - here's a fixed version of your code (untested):

    final ContentResolver cr = getContentResolver();
    String[] projection = new String[] {Contacts.DISPLAY_NAME, Phone.NUMBER};
    final Cursor c = cr.query(Data.CONTENT_URI, projection, null, null, null);
    myCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {Phone.NUMBER}, new int[]{R.id.TVRow}, 0);
    myPhoneList.setAdapter(myCursorAdapter);
    
    myPhoneList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id){
            c.moveToPosition(position);
            Toast.makeText(getApplicationContext(), c.getString(1), Toast.LENGTH_SHORT).show();
        }
    });
    

提交回复
热议问题