Mr. K
Mr. K

Reputation: 21

Address format in Honeycomb / ICS

I have an app which can retrieve the mailing address from contact information on the device. I retrieve the information for street address, post code, city etc. using the contacts API using code similar to below. I then display the address on separate lines and the user can accept it or change it if desired.

String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
String[] params = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}; 
Cursor addrCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null); 
...

while(addrCur.moveToNext()) {
    String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
    String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
    String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
    String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
    String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
    ...
}       
addrCur.close();

This has worked fine with Android 2.2/2.3 but I noticed on 3.x and higher that the entire address that is returned can be formatted as a single string with line breaks in the STREET field. This occurs when the contact/address is created on the device; if I sync a contact from Gmail, the address is retrieved correctly (I suspect that the synced info is saved in the correct fields). I can't see any way to get the individual data elements (street, postcode, etc) for these types of addresses. Is this possible? Is there a new API I need to use?

Upvotes: 2

Views: 508

Answers (1)

Eric Brynsvold
Eric Brynsvold

Reputation: 3090

I haven't found a way to do it. The only workaround I've found is to get the full, formatted address by getting the ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS column from the cursor and then attempt to break it apart assuming a couple different standard formattings. There's no way to input an address in the Honeycomb/ICS Contacts app other than using a free-form text box, so I guess the Contacts app doesn't try and parse it automatically before storing it in the database.

Hopefully I'm wrong and there is a way to do it - but I haven't found it yet.

Upvotes: 1

Related Questions