2

I'm displaying a list of Contacts, and have a context menu to Edit Contact by calling an intent. On some contacts it works fine, but on others the Edit Contact activity is blank. Any ideas?

Here is the cursor...

 projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID};   
 uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
 cursor = getActivity().getContentResolver().query(uri, projection, null, null,    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

Here is the code from my CursorAdapter.getView() ...

textView.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)) ;

And here is the code from my onContextItemSelected...

cursor.moveToPosition(position);
String idContact = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Intent i = new Intent(Intent.ACTION_EDIT);
i.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + idContact));
parent.startActivity(i);

I've checked logcat and can see

I/ActivityManager(  102): Starting activity: Intent { act=android.intent.action.EDIT dat=content://com.android.contacts/contacts/lookup/23356 cmp=com.android.htccontacts/.ui.EditContactActivity }

but no error messges

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • any pattern you noticed with the contacts that dont show up? Didi you try debugging it? – Ron Nov 20 '12 at 13:56
  • none I can discern :-( On the emulator I simply added 3 contacts, 2 work, one doesn't. On a real device, I get nothing whatsoever. I can't find anything to debug. The code is executing correctly, with no silent exceptions. I can see the intent being fired. After that point, it's up to Android to do the rest. My guess is there is something wrong with the CONTENT_LOOKUP_URI, or the way I'm deriving the contact ID. – pinoyyid Nov 20 '12 at 14:51

1 Answers1

4

Try this:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = this.getContentResolver().query(uri, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
long idContact = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

then

Intent i = new Intent(Intent.ACTION_EDIT);
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, idContact); 
i.setData(contactUri);
pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • Seems to work perfectly. I'll do some more testing and then accept the answer. Many thanks. – pinoyyid Nov 21 '12 at 02:49
  • I'm glad I helped. The main problem was `_ID` instead of `CONTACT_ID`. Contacts data are stored in multiple tables and `_ID` is an id in one of those tables. – pawelzieba Nov 21 '12 at 10:14