显示联系号码

时间:2011-09-15 11:40:35

标签: android android-contacts

我已将此代码引用以显示联系人列表。它工作正常并显示名称。但我想显示数字。我应该在这段代码中改变什么?

  protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);       
              Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
              startActivityForResult(intentContact, PICK_CONTACT);
this.infonumber();
        }//onCreate

        public void onActivityResult(int requestCode, int resultCode, Intent intent){
            if (requestCode == PICK_CONTACT)
            {         
                getContactInfo(intent);         
                // Your class variables now have the data, so do something with it. 
            }
        }//onActivityResult

        protected void getContactInfo(Intent intent){
            Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
            while (cursor.moveToNext()) {           
                String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
                String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                if ( hasPhone.equalsIgnoreCase("1"))
                    hasPhone = "true";
                else
                    hasPhone = "false" ;

                if (Boolean.parseBoolean(hasPhone)){
                    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                    while (phones.moveToNext()) {
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }
                    phones.close();
               }

               // Find Email Addresses
               Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);
               while (emails.moveToNext()) {
                   String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
               }
               emails.close();

            Cursor address = getContentResolver().query(
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId,
                    null, null);

            /*while (address.moveToNext()) 
            { 
              // These are all private class variables, don't forget to create them.
                String poBox = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                String street = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                String city = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                String state = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                String postalCode = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                String country = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                String type = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
            } //address.moveToNext()*/   

            }  //while (cursor.moveToNext())        
            cursor.close();
        }//getContactInfo

public void infonumber(){
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
        if (cur.getCount() > 0) 
        {
            while (cur.moveToNext()) 
            {
                id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                            new String[]{id}, null);

                    while (pCur.moveToNext()) 
                    {
                        for(int i=0;i<pCur.getColumnCount();i++)
                        {
                            // you can get the value by using 
                            val = pCur.getString(i);
                        } 
                        pCur.close();
                        pCur = null;
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

这是代码试试这个

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                Cursor pCur = cr.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                new String[]{id}, null);

                while (pCur.moveToNext()) {
                    for(int i=0;i<pCur.getColumnCount();i++)
                                       // you can get the value by using 
                                       String val=pCur.getString(i); ///
                } 
                pCur.close();
                pCur = null;
             }

        }
}