Android – Get all contacts and their details (e.g. postal address) in one OUTER JOIN query

androidcontactscontactscontract

I know how to retrieve contact data for specific contacts. However, i can't find a way to get all contacts plus some of their details in a single query. The following code gets all contacts having a postal address:

  Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
  String[] projection = new String[] {
    StructuredPostal._ID,
    StructuredPostal.LOOKUP_KEY,
    StructuredPostal.DISPLAY_NAME,
    StructuredPostal.STREET,
    StructuredPostal.CITY
  };
  String sortOrder = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
  Cursor c = getContentResolver().query(uri, projection, null, null, sortOrder);

But what i need are all contacts, whether they have postal address or not. Is this doable using the ContatsContract API, or do i need to create custom outer join query? Any hints on how?

Best Answer

if You have a contact ID and you want to fetch the Postal Address then use this :

         Uri postal_uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
         Cursor postal_cursor  = getContentResolver().query(postal_uri,null,  ContactsContract.Data.CONTACT_ID + "="+contactId.toString(), null,null);
          while(postal_cursor.moveToNext())
            {
             String Strt = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.STREET));
             String Cty = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.CITY));
             String cntry = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.COUNTRY));
            } 
            postal_cursor.close();                   
Related Topic