2013-02-18 45 views
0

我無法訪問我的聯繫人的電話號碼,無法弄清楚原因。Android,無法訪問聯繫人電話號碼

private void getContacts(){ 
     Cursor people = mCon.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
     if (people.moveToFirst()) { 
      do { 
       int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
       String contact = people.getString(nameFieldColumnIndex).toLowerCase(); 
       int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER); 
       String number = people.getString(numberFieldColumnIndex); 
       hm.put(contact, number); 
      }while(people.moveToNext()); 
     } 
     people.close(); 
    } 

名稱正在進入HashMap的罰款,但numberFieldColumnIndex不斷出來爲-1

我輸出的人列標題,並沒有一個號碼是多少?我如何檢索它們?人們

02-18 13:26:38.500: D/Contacts(1587): 0 times_contacted 
02-18 13:26:38.500: D/Contacts(1587): 1 contact_status 
02-18 13:26:38.500: D/Contacts(1587): 2 phonetic_name 
02-18 13:26:38.500: D/Contacts(1587): 3 phonetic_name_style 
02-18 13:26:38.500: D/Contacts(1587): 4 link 
02-18 13:26:38.500: D/Contacts(1587): 5 is_user_profile 
02-18 13:26:38.500: D/Contacts(1587): 6 raw_contact_linkpriority4 
02-18 13:26:38.500: D/Contacts(1587): 7 raw_contact_linkpriority5 
02-18 13:26:38.500: D/Contacts(1587): 8 lookup 
02-18 13:26:38.500: D/Contacts(1587): 9 contact_status_icon 
02-18 13:26:38.500: D/Contacts(1587): 10 last_time_contacted 
02-18 13:26:38.500: D/Contacts(1587): 11 sec_custom_vibration 
02-18 13:26:38.500: D/Contacts(1587): 12 _id 
02-18 13:26:38.500: D/Contacts(1587): 13 display_name_source 
02-18 13:26:38.500: D/Contacts(1587): 14 photo_uri 
02-18 13:26:38.500: D/Contacts(1587): 15 photo_thumb_uri 
02-18 13:26:38.500: D/Contacts(1587): 16 contact_chat_capability 
02-18 13:26:38.500: D/Contacts(1587): 17 photo_id 
02-18 13:26:38.500: D/Contacts(1587): 18 send_to_voicemail 
02-18 13:26:38.500: D/Contacts(1587): 19 display_name_reverse 
02-18 13:26:38.500: D/Contacts(1587): 20 custom_ringtone 
02-18 13:26:38.500: D/Contacts(1587): 21 name_raw_contact_id 
02-18 13:26:38.500: D/Contacts(1587): 22 photo_file_id 
02-18 13:26:38.500: D/Contacts(1587): 23 has_phone_number 
02-18 13:26:38.500: D/Contacts(1587): 24 link_type5 
02-18 13:26:38.500: D/Contacts(1587): 25 link_type4 
02-18 13:26:38.500: D/Contacts(1587): 26 link_type3 
02-18 13:26:38.500: D/Contacts(1587): 27 contact_status_label 
02-18 13:26:38.500: D/Contacts(1587): 28 link_type2 
02-18 13:26:38.500: D/Contacts(1587): 29 link_type1 
02-18 13:26:38.500: D/Contacts(1587): 30 raw_contact_linkpriority3 
02-18 13:26:38.500: D/Contacts(1587): 31 raw_contact_linkpriority2 
02-18 13:26:38.500: D/Contacts(1587): 32 display_name 
02-18 13:26:38.500: D/Contacts(1587): 33 raw_contact_linkpriority1 
02-18 13:26:38.500: D/Contacts(1587): 34 has_email 
02-18 13:26:38.500: D/Contacts(1587): 35 sort_key_alt 
02-18 13:26:38.500: D/Contacts(1587): 36 dirty_contact 
02-18 13:26:38.500: D/Contacts(1587): 37 in_visible_group 
02-18 13:26:38.500: D/Contacts(1587): 38 starred 
02-18 13:26:38.500: D/Contacts(1587): 39 link_count 
02-18 13:26:38.500: D/Contacts(1587): 40 display_name_alt 
02-18 13:26:38.500: D/Contacts(1587): 41 sort_key 
02-18 13:26:38.500: D/Contacts(1587): 42 contact_presence 
02-18 13:26:38.500: D/Contacts(1587): 43 contact_status_res_package 
02-18 13:26:38.500: D/Contacts(1587): 44 contact_status_ts 

回答

0

列表的列標題下面的代碼顯示了一個簡單的方法來讀取所有的電話號碼和姓名:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
while (phones.moveToNext()) 
{ 
    String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

} 
phones.close(); 
相關問題