Search code examples
androidcontactsandroid-contacts

ContactsContract how to enumerate all available fields?


In my application I need to give user opportinity to see/edit all available fields of ContactsContract.

How should I get/see all available fields - including any custom ones?


Solution

  • First get all of the names of people that exist in your phone:

    Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        null,
                        null,
                        ContactsContract.Contacts.DISPLAY_NAME
                                + " COLLATE LOCALIZED ASC");
                while (phones.moveToNext()) {
    
                    name = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    namelist.add(name);
                    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                     number_list.add(phoneNumber);
                detaillist.put(name, phoneNumber);
                }
                phones.close();
    }
    

    Now get contact id and get other information like phone number, email id, address, organization by using contact id:

    protected void get_UserContactId(String item_clicked) 
            {
                System.out.println("i m in fincution contact id");
                ContentResolver localContentResolver = this.getContentResolver();
                Cursor contactLookupCursor =  
                   localContentResolver.query(
                            Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
                            Uri.encode(item_clicked)), 
                            new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
                            null, 
                            null, 
                            null);
                try {
                while(contactLookupCursor.moveToNext()){
                  contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
                    user_contact_id = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
    
                     System.out.println("contatc id id"+user_contact_id);
    
                    }
    
                }
            finally {
                contactLookupCursor.close();
                } 
    
            }
    
    
    protected void get_UserEmail(String item_clicked) 
            {
                Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user_contact_id, null, null); 
                while (emails.moveToNext()) { 
                   // This would allow you get several email addresses 
                   user_email_id = emails.getString( 
                   emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
                   String user_email_type=emails.getString( 
                           emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
                   System.out.println("the email type"+user_email_type);
                   System.out.println("email address might be"+emailAddress);
                contact_attribute_type.add(EMAIL_TYPE[(Integer.parseInt(user_email_type))-1]);
                contact_attribute_value.add(user_email_id);
                } 
                emails.close(); 
    
            }
    
            protected void get_UserNumber(String item_clicked) {
                // TODO Auto-generated method stub
                final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
                    ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts.STARRED,
                    ContactsContract.Contacts.TIMES_CONTACTED,
                    ContactsContract.Contacts.CONTACT_PRESENCE,
                    ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.Contacts.LOOKUP_KEY,
                    ContactsContract.Contacts.HAS_PHONE_NUMBER,
                };
    
    
    
                String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +item_clicked+ "\" )";
                Cursor c = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
                this.startManagingCursor(c);
    
                if (c.moveToNext())
                {
                    String id = c.getString(0);
                    ArrayList<String> phones = new ArrayList<String>();
    
                    Cursor pCur = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                    while (pCur.moveToNext())
                    {
                        phones.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                        user_number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                       String number_type=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                       System.out.println("the number type---"+number_type); 
                       System.out.println("user no. in share is"+user_number);
                       contact_attribute_type.add(PHONE_TYPE[(Integer.parseInt(number_type))-1]);
                       contact_attribute_value.add(user_number);
                     //   Log.i("", name_to_search+ " has the following phone number "+ pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
    
                    } 
                    pCur.close();   
                }
    
                }
    
    
            protected String get_UserAddress() {
                // TODO Auto-generated method stub
                try { 
    
                    System.out.println(" i m in user address");
                Cursor address = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + client_contact_id, null, null); 
                    while (address.moveToNext()) { 
                       // This would allow you get several email addresses 
                      user_home_address = address.getString( 
                       address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA)); 
                      String user_address_type=address.getString( 
                               address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
                      System.out.println("the user address type"+user_address_type);
                      System.out.println(" address might be"+user_home_address);
                    contact_attribute_type.add(ADDRESS_TYPE[(Integer.parseInt(user_address_type))-1]);
                    contact_attribute_value.add(user_home_address);
    
                    } 
                    address.close(); 
                }
                catch(Exception e)
                {
                    System.out.println("this exception due to website"+e);
                }
    
            return(user_home_address);  
            }   
    
    
              protected void get_UserWebsite() {
                    // TODO Auto-generated method stub
                            try{
                                Cursor websiteNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] {Website.URL}, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
                                        + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE
                                        + "'",null,null);
    
                                websiteNameCursor.moveToNext();
                                user_website=(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL)));
    
                                System.out.println("this is my website"+(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL))));
                            contact_attribute_type.add("Website");
                            contact_attribute_value.add(user_website);
    
                            }
                                catch(Exception e)
                                {
                                user_website=null;
                                System.out.println("this website is"+user_website); 
                                System.out.println("this exception in website"+e);
                                }
    
    
    
                }
    
                        protected void get_UserOrganization() {
                    // TODO Auto-generated method stub
                            try{
                                Cursor organizationNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] {Organization.COMPANY}, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
                                        + ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE
                                        + "'",null,null);
    
                                organizationNameCursor.moveToNext();
                                user_company=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.COMPANY));
                            //    String user_company_type=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.TYPE));
                           //   System.out.println("the company type "+user_company_type);
                                System.out.println("this is my organization"+user_company);
    
                                contact_attribute_type.add("Company");
                            contact_attribute_value.add(user_company);
                            }
                                catch(Exception e)
                                {
                                user_company=null;
                                System.out.println("user company name is"+user_company);
                                    System.out.println("this exception in org"+e);
                                }
                }   
    
    
    
    
        }
    

    By doing this you can get all the fields of contacts-contract.