简体   繁体   English

在Android 2.2中检索联系人

[英]retriveing contacts in android 2.2

Hi i need to get contact details in edittext when i press a button.But i am getting it right when i have only one contact and it is showing an execption if it has more than one contact.Here is my code & i dont know what should i add to it.So help if anyone knows.Thanks in Adv. 嗨,当我按下一个按钮时,我需要在edittext中获取联系人详细信息。但是当我只有一个联系人时,如果它有多个联系人,它将显示执行,这是我的正确代码,我不知道该怎么办我应该添加到它。所以如果有人知道,请帮忙。感谢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(); 

你可以访问联系人信息 ,所有联系人和信息查询

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM