在Android中阅读联系人

时间:2010-10-05 13:31:11

标签: android

我正在编写用于从android中读取联系人的程序。当我执行以下代码时,它成功获取了名称但未能获取电话号码并显示ArrayIndexoutofBoundException ....代码是

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

while(people.moveToNext()) 
{
    int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
    String contact = people.getString(nameFieldColumnIndex);
    contactView.append(contact);

    int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);

    String number = people.getString(numberFieldColumnIndex);
    contactView.append(number);
}

1 个答案:

答案 0 :(得分:1)

http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

boolean hasPhone = (Integer.parseInt(people.getString(
                       people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0);  
 if (hasPhone)  
 {
     Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                   null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                   new String[]{id}, null);
     while (pCur.moveToNext()) {
        // process phone numbers
     } 
     pCur.close();
 }