Question

G'day,

I am using a Cursor + contentResolver to query various fields to get contact information in Android. For each field, I have a json object defined too. In the snippet below orgObj is a JSONObject. Here is my sample code:

    String[] filter = {contactID};

      Cursor genericCursor = cr.query(Data.CONTENT_URI,  
                    null, Data.CONTACT_ID + "=?", filter, null);
            while(genericCursor.moveToNext())
            {
                String organisation = genericCursor.getString(genericCursor.getColumnIndex(CommonDataKinds.Organization.COMPANY));
                String title = genericCursor.getString(genericCursor.getColumnIndex(CommonDataKinds.Organization.TITLE));


                if ((organisation != null) && (title != null))
                {

                    orgObj.put(organisation, title);
                    Log.v(TAG, "org is " + organisation.toString() + " title is " + title.toString());
                }

This works fine by the way. But what it tends to do is also query the address for the contact for some reason? The Log.v output is follows:

02-08 20:25:12.850: V/GetContactsDataNew(2171): org is 123, NY Way, NY 309210 title is 123, NY Way, NY 309210 02-08 20:25:12.850: V/GetContactsDataNew(2171): org is 1249 NY way, NYC 9192 title is 1249 NY way, NYC 9192 02-08 20:25:12.850: V/GetContactsDataNew(2171): org is Google Inc title is Engineer

Only the last Log.v output is correct and I cannot for the life of me figure out why it's appending the address for the contact? I can understand that it appends the work address (even though it shouldn't, not under the Organization.COMPANY and Organization.Title fields), but the second address is actually a home address :/

I'd love me some help :)

Was it helpful?

Solution

Well, two hours and I finally figured out what was wrong. It was the mimetype for each contact property (or what have you).

Basically, because we seem to only be able to query the Data.CONTENT_URI, we need to validate against the MIMETYPE for the particular property. In my case, the following snipped fixed it :D

    while (orgCursor.moveToNext())
            {
                String organisation = orgCursor.getString(orgCursor.getColumnIndex(CommonDataKinds.Organization.DATA1));
                String title = orgCursor.getString(orgCursor.getColumnIndex(CommonDataKinds.Organization.DATA4));
                String MIMETYPE = orgCursor.getString(orgCursor.getColumnIndex(CommonDataKinds.Organization.MIMETYPE));



                if ((organisation != null) && (title != null)&&(MIMETYPE.equals("vnd.android.cursor.item/organization")))
               {
                    orgObj.put(organisation, title);
                    Log.v(TAG, "org is " + organisation.toString() + " title is " + title.toString());
               }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top