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 4036236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:11:55+00:00 2026-05-20T12:11:55+00:00

I’m trying to add Data record to an already exist contact, I find the

  • 0

I’m trying to add Data record to an already exist contact, I find the contact using phone lookup, i take the contact _id field, and add a new data with raw_contact_id set to the _id field.
on some contacts it just doesn’t work, it match the data to different contact.
(I think it relates to contacts that are stored on the sim card)

Please advice, maybe you have a different way to add the data

code sample:

LinkedList<Long> lcv = new LinkedList<Long>();
    ContentResolver cr = getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor c = cr.query(uri, null, null, null, null);
    try {
        while (c.moveToNext()) {
            Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI,
                    c.getString(c.getColumnIndex(PhoneLookup.LOOKUP_KEY)));
            Cursor c2 = getContentResolver().query(lookupUri, new String[] { Contacts._ID, Contacts.DISPLAY_NAME },
                    null, null, null);
            try {
                if (c2.moveToNext()) {
                    Log.i(TAG, "found: " + c2.getLong(c2.getColumnIndex(Contacts._ID)) + ", " + c2.getString(c2.getColumnIndex(Contacts.DISPLAY_NAME)));
                    lcv.add(c2.getLong(c2.getColumnIndex(Contacts._ID)));
                } else {
                    Log.e(TAG, "failed to lookup");
                }
            } finally {
                c2.close();
            }
        }
    } finally {
        c.close();
    }

    for (Long rawid : lcv) {
                        Cursor c3 = cr.query(RawContacts.CONTENT_URI, null, RawContacts.CONTACT_ID + "=?", new String[] {rawid+""}, null);
                        if (c3.moveToNext()) {
                            Log.e(TAG,"aaaa: " + c3.getString(c3.getColumnIndex(Contacts.DISPLAY_NAME)));
                        } else {
                            Log.e(TAG,"errrrror");
                        }

                        ContentValues cv = new ContentValues();
                        cv.put(Data.RAW_CONTACT_ID, rawid + "");
                        cv.put(Data.MIMETYPE, MyMime.MIMETYPE);
                        cv.put(Data.DATA1, "mydata");
                        cv.put(Data.SYNC1, syncvalue);
                        Uri newIns = cr.insert(ContactsContract.Data.CONTENT_URI, cv);
                        Log.i(TAG, "insert: " + newIns + ", " + name);
                    }

  • 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-20T12:11:55+00:00Added an answer on May 20, 2026 at 12:11 pm

    The problem lies when you select the Contacts._ID and use this id to populate the data in the LinkedList lcv .

     Cursor c2 = getContentResolver().query(lookupUri, new String[] { Contacts._ID, Contacts.DISPLAY_NAME },
                        null, null, null);
    

    You actually need a RAW_CONTACT_ID here.

    The DISPLAY_NAME can be fetched either from Contacts database/ContactsContract.Data' OR 'database/ContactsContract.CommonDataKinds.StructuredName' OR 'database/RawContactsEntity. In the later 2 cases you will be able to fetch the DISPLAY_NAME using RAW_CONTACT_ID

    Couple of Key pointers:

    • Contacts._ID = Data.CONTACT_ID
    • RawContacts._ID = Data.RAW_CONTACT_ID
    • RawContacts.CONTACT_ID = Contacts._ID
    • RawContactsEntity._ID = RawContacts._ID

    Sounds confusing?? Let me try…

    1. The Contacts database is divided into 3 tables contacts, raw contacts, and data.
    2. Each table contains column (_ID) which is an auto incremented primary key.
    3. data table contains all the contact info like phone number, mail id, address etc.
    4. The raw contacts points to the actual contact created. Hence we use the raw contacts while adding a contact.
    5. The user cannot add any data in the contacts table. The data in this table is populated internally due to aggregation of contacts.
    6. The reason your logic worked for some of the contacts is: _ID for contacts, raw contacts remains same until there is any contact aggregation taking place. Lets say you add two contacts with same name abc. Here the _ID for raw contacts increments twice while _ID for contacts increments only once as these two contacts gets merged due to the aggregation of contacts

    Refer this for more details.

    The best approach to fetch the info in your case is by using ContactsContract.RawContactsEntity ( an outer join of the raw_contacts table with the data table)

    Reference: http://developer.android.com/reference/android/provider/ContactsContract.RawContactsEntity.html

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
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

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.