I'm trying to create a class, its fields being Contact Info (name, address, etc).
public class ContactObject {
private Activity actividad;
private Uri contactUri;`enter code here`
// variable para asegurarse que la consulta de contacto se realizó correctamente
private boolean isOK = true;
private String displayName;
private String givenName;
private String familyName;
private String region;
private String postcode;
private String city;
private String street;
public ContactObject(Activity actividad, Uri contactUri) {
super();
this.actividad = actividad;
this.contactUri = contactUri;
Cursor c = null;
try{
String[] selection = new String[]{
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
ContactsContract.CommonDataKinds.StructuredPostal.REGION,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.Contacts.DISPLAY_NAME
};
c = actividad.getContentResolver().query(contactUri, selection,null, null, null);
if (c != null && c.moveToFirst()) {
this.street = c.getString(0);
this.city = c.getString(1);
this.postcode = c.getString(2);
this.region = c.getString(3);
this.givenName = c.getString(4);
this.familyName = c.getString(5);
this.displayName = c.getString(6);
}
}catch(Exception e){
CLog.e("peta constructor ContactObject: " + e.getMessage());
isOK = false;
}
}
// getters and setters
}
// code to launch contact picker
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
// code to create the ContactObject
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Uri uri = data.getData();
if (uri != null) {
ContactObject co = new ContactObject(actividad, uri);
String mensaje;
if (co.isOK()){
mensaje = "Contacto encontrado: " + co.getDisplayName();
}else{
mensaje = "error recuperando datos del contacto " + uri.toString();
}
CToast.mensajeLargo(mensaje, actividad.getApplicationContext());
}
}
}
}
In the emulator I created several contacts (only name & phone). In the testing app, I call the contactPicker onResult and, with the contact uri, create the ContactObject. When I execute it, it throws an exception, its message "invalid column data4".
As I've collected, "data4" is the value for the constant ContactsContract.CommonDataKinds.StructuredPostal.STREET. However, if I only query for the DISPLAY_NAME I get it without problem.
I know that these contacts do not have an address, but then I have no addresses on many of my (actual) phone contacts and the apps do not crash just because of it.
What am I doing wrong?