检索android 2.2中的联系人

时间:2012-07-04 05:29:46

标签: android android-edittext android-contacts

当我按下按钮时,我需要在edittext中获取联系人详细信息。但是当我只有一个联系人并且如果它有多个联系人时它正在显示一个execption我正在做对。这是我的代码&我不知道应该添加什么。如果有人知道,请提供帮助。感谢Adv。

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

            while (cursor.moveToNext()) {
                String contactId = cursor.getString(cursor.getColumnIndex( 
                       ContactsContract.Contacts._ID)); 



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

                  e2.setText(phoneNumber);
                  } 

               //}

            Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 
            startManagingCursor(emails); 
            while (emails.moveToNext()) { 
               // This would allow you get several email addresses 
               String emailAddress = emails.getString( 
               emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
               e3.setText(emailAddress);
            } 

            Cursor street = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, 
                    null,
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId, null, null); 
            startManagingCursor(street); 
            while (street.moveToNext()) { 
                String streetname=street.getString(street.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                e1.setText(streetname);
            }
            phones.close(); 
            emails.close(); 
            street.close();
            cursor.close(); 

2 个答案:

答案 0 :(得分:0)

您可以访问查询所有联系人和信息的Contact Information

答案 1 :(得分:0)

while(!cursor.isAfterLast())更改您的同时条件。