Frank
Frank

Reputation: 2837

Invalid column data4 in contacts cursor

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?

Upvotes: 1

Views: 457

Answers (2)

Frank
Frank

Reputation: 2837

I got it to work by changing the intent to launch the contact picker:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
        startActivityForResult(intent, CONTACT_PICKER_RESULT); 

Using this one and testing it on my (actual) phone it demands me to choose an action to use: either file manager, contacts or (since it's installed) dropbox. Choosing contacts makes it work OK, I get all the info from the selected contact. I wonder, however, if there's not a way to do this calling specifically the contact picker and not leaving it to the user's choice?

Upvotes: 0

Joe Malin
Joe Malin

Reputation: 8641

What content URI are you using?

Your projection contains column names from three different places: StructuredPostal StructuredName Contacts

Your error says that you're trying to get a column name in a table that doesn't have that column name.

In particular, I can see that ContactsContract.CommonDataKinds.StructuredPostal.STREET = "data4". If your query is not against the table ContactsContract.DATA, you're going to have an error.

You may want to go back at look at the Contacts Provider documentation.

Upvotes: 1

Related Questions