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

Deadly 提交于 2019-12-04 04:17:54

问题


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?


回答1:


All contact information in Android 2.0 is stored in a single database table. So you can get all the information you need in a single query:

Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
    null, null, null, sortOrder);

The just iterate through the data and check Data.MIMETYPE column. For example, if this column has StructuredPostal.CONTENT_ITEM_TYPE value, then you can get StructuredPostal fields from this column.




回答2:


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


来源:https://stackoverflow.com/questions/5848573/get-all-contacts-and-their-details-e-g-postal-address-in-one-outer-join-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!