tags:

views:

47

answers:

1

i need to read organization name from phone contacts in 2.1 i can read name and phone number using following code

         Cursor cur = User.cr.query(ContactsContract.Contacts.CONTENT_URI, null,  
                 null, null, null);  

         if (cur.getCount() > 0) {  

             while (cur.moveToNext()) {  

                 // ID AND NAME FROM CONTACTS CONTRACTS  
                 String id = cur.getString(cur  
                         .getColumnIndex(ContactsContract.Contacts._ID));  
                 String name = cur  
                         .getString(cur  
                                 .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));  

   //              Log.i("Pratik", "ID :" + id);  
    //             Log.i("Pratik", "NAME :" + name);  

                 // GET PHONE NUMBERS WITH QUERY STRING  

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

                     // WHILE WE HAVE CURSOR GET THE PHONE NUMERS  
                   HashMap tempMap=new HashMap();
                     while (pCur.moveToNext()) {  
                         // Do something with phones  
                          phone = pCur  
                                 .getString(pCur  
                                         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));  

                         String phoneType = pCur  
                                 .getString(pCur  
                                         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));  


                       tempMap.put(phoneType, phone);
                     }  
                    pCur.close();
                    if(name!=null&&tempMap!=null)
                  if (!(Pattern.matches("^[A-Za-z0-9.\\-]+.[A-Za-z0-9.\\-]+\\.[A-Za-z]{2,4}$",name)))
                  { User.provider_name_phoneno.put(name, tempMap);
                    User.phoneContactName.add(name);
                 } 
                 }

             }  
         }  
A: 

Organization has its own MIME type, so you'll also need to query for that.. This should help:

public Organization getContactOrg(String id) {
    Organization org = new Organization();
    String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] whereParameters = new String[]{id, 
            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; 

    Cursor orgCur = this.cr.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);

    if (orgCur.moveToFirst()) { 
        String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
        String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
        if (orgName.length() > 0) {
            org.setOrganization(orgName);
            org.setTitle(title);
        }
    } 
    orgCur.close();
    return(org);
}
Dave
Thanks Dave it's working
Sudeep SR