2010-10-28 4 views
0

i besoin de lire le nom de l'organisation des contacts téléphoniques en 2.1 je peux lire le nom et le numéro de téléphone en utilisant le code suivanttéléphone android contacts

  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); 
       } 
       } 

      } 
     } 

Répondre

0

Organisation a son propre type MIME, vous aurez également besoin d'interroger pour cela .. Cela devrait aider:

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); 
} 
+0

Merci Dave ça marche – Bytecode

Questions connexes