3

in my app (android 4.0) i need to retrieve the address from a contact. I used the solution provided in another post here:

Uri contactData = data.getData();
Cursor c =  managedQuery(contactData, null, null, null, null);
    if (c.moveToFirst()) {
        String city = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));}

But when i try to execute this, i get an error:

"Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 29 columns. "

What am i doing wrong here? Why doesn't he find the city column?

Jim Panse
  • 159
  • 2
  • 11
  • 1
    There is no column named `ContactsContract.CommonDataKinds.StructuredPostal.CITY` in your cursor – sinisha Dec 08 '12 at 13:25
  • So what's the "Mimetype" to get the address? "ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS" also doesn t work although it seems to work here: [link](http://stackoverflow.com/questions/3609700/get-postal-address-from-a-contact-using-contactscontract-api-on-android?rq=1) – Jim Panse Dec 08 '12 at 13:34
  • Try to use Thorstenvv answer from that link – sinisha Dec 08 '12 at 13:52

1 Answers1

3

Ok, solved it, got some stupid mistakes... Here is the working code:

manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="androidlab.exercise4_1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.READ_CONTACTS"/> ...

Code:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_TYPE); 
startActivityForResult(intent, PICK_CONTACT);




public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (PICK_CONTACT) :
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c =  managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    try{
                        //String street = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));//ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                        //String city = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                        //String postcode = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                        //String country = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                        String street = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                    }
                    catch(Exception e){
                        errorDialog.show();
                    }
                }
            }
Jim Panse
  • 159
  • 2
  • 11