How to get only Contacts with a StructuredAddress attached in Android?
By executing the following code i can parse all Android contact book items and attached postal address:
Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor addresses = mContext.getContentResolver().query( ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID +" = "+ contactId, null, null);
while (addresses.moveToNext()) {
String city = addresses.getString(addresses.getColumnIndex( ContactsContract.CommonDataKinds.Stru开发者_Go百科cturedPostal.CITY));
// other structured address fields.
}
}
I am not interested in address-less entries, does anybody knows if i can select only address book items that have a a structured address in the first query, so that i don't have to waste time parsing all of them ?
Yes, there is. I have also been looking for quite some time to get it fast and correct. I came up with this:
private ArrayList<CustomAddressObject> getPostalAddresses(){
Cursor postals = null;
ArrayList<CustomAddressObject> results = null;
try{
postals = getContentResolver().query(
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null,
null,
null,
null);
int postFormattedNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
int postTypeNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
int postLabelNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.LABEL);
int postNameNdx = postals.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
if(postals.getCount() == 0){
//No contacts with addresses
return null;
}
results = new ArrayList<CustomAddressObject>();
while (postals.moveToNext()) {
CustomAddressObject address = new CustomAddressObject ();
address.address = postals.getString(postFormattedNdx);
address.contactName = postals.getString(postNameNdx);
Log.d(TAG, "Contact name: "+address.contactName);
if(address.address != null && address.contactName != null){
switch (postals.getInt(postTypeNdx)) {
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM:
address.label = postals.getString(postLabelNdx);
if(address.label == null){
address.label = getString(R.string.contacts_type_other);
}
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
address.label = getString(R.string.contacts_type_home);
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
address.label = getString(R.string.contacts_type_work);
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER:
address.label = getString(R.string.contacts_type_other);
break;
default:
address.label = null;
}
if(!results.contains(address))
results.add(address);
}else{
//Some phones can store some information inside the postals that don't actually contain an address...
Log.d(TAG, "Contact has no address with id : "+contactId);
Log.d(TAG, "Contact has no address with name: "+getContactName(contactId));
}
}
}
catch (IllegalStateException e) {
// TODO: handle exception
}finally{
if(postals != null && !postals.isClosed()){
postals.close();
}
}
return results;
}
精彩评论