Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7247887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:03:56+00:00 2026-05-28T22:03:56+00:00

I’ve worked hard on the following code, but unfortunately, the entry is assigned to

  • 0

I’ve worked hard on the following code, but unfortunately, the entry is assigned to the wrong contact. I don’t know why. Tested for hours days but can’t find the mistake. Can you help me?

I would like to use the code in order to select a person from the contact list (using the contact picker) and then adding an event entry (date of birth) for this person to the contacts table.

Step 1:

I’ve already set the permission in the manifest file:

<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>

Step 2:

The contact picker’s ID is defined:

private static final int CONTACT_PICKER_ID = 123;

Step 3:

The contact picker is called:

Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_ID);

Step 4:

Another method listens for the contact picker’s result and tries to add an event for the selected user:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
            case CONTACT_PICKER_ID:
                Uri selectedPerson = data.getData();
                String contactId = selectedPerson.getLastPathSegment();
                // ADD A NEW EVENT FOR THE SELECTED CONTACT --- BEGIN
                ContentValues values = new ContentValues();
                values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
                values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
                values.put(ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID, contactId);
                values.put(ContactsContract.CommonDataKinds.Event.LABEL, "");
                values.put(ContactsContract.CommonDataKinds.Event.START_DATE, "2010-01-28"); // hard-coded date of birth
                Uri created = null;
                try {
                    created = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
                }
                catch (Exception e) {
                }
                if (created == null) {
                    Toast.makeText(this.getApplicationContext(), "Failed inserting the event!", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(this.getApplicationContext(), "Successfully inserted the event!", Toast.LENGTH_SHORT).show();
                }
                // ADD A NEW EVENT FOR THE SELECTED CONTACT --- END
            break;
        }
    }
}

The event is successfully inserted to the database and also shown in the Google contacts – but unfortunately it’s assigned to the wrong contact. Why is this so? Is my contactId wrong that I get from the contact picker?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:03:57+00:00Added an answer on May 28, 2026 at 10:03 pm

    The activity result that you get back from the contact picker is the full path to the contact. Something like:

    content://com.android.contacts/contacts/lookup/0r7-2C46324E483C324A3A484634/7
    

    This is what’s in your:

    Uri selectedPerson = data.getData();
    

    This contains both the Contact’s LOOKUP_KEY AND the Contact’s _ID. However, you need to be using the RawContacts _ID when inserting into the Data table. What you need to do is grab the RawContact’s _ID:

    long rawContactId = -1;
    Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
          new String[]{RawContacts._ID},
          RawContacts.CONTACT_ID + "=?",
          new String[]{String.valueOf(contactId)}, null);
    try {
        if (c.moveToFirst()) {
            rawContactId = c.getLong(0);
        }
    } finally {
        c.close();
    }
    

    And then use the rawContactId:

    values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
    

    However, it should be noted that there can be multiple RawContacts per one Contact. You may want to adjust your code so that it adds an event for each RawContact.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.