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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:29:41+00:00 2026-06-18T22:29:41+00:00

Whenever I want to add new data to an existing Android contact, I use

  • 0

Whenever I want to add new data to an existing Android contact, I use the following function to retrieve all RawContacts IDs for the given contact ID:

protected ArrayList<Long> getRawContactID(String contact_id) {
    ArrayList<Long> rawContactIDs = new ArrayList<Long>();
    String[] projection = new String[] { ContactsContract.RawContacts._ID };
    String where = ContactsContract.RawContacts.CONTACT_ID + " = ?";
    String[] selection = new String[] { contact_id };
    Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, where, selection, null);
    try {
        while (c.moveToNext()) {
            rawContactIDs.add(c.getLong(0));
        }
    }
    finally {
        c.close();
    }
    return rawContactIDs;
}

After that, I just insert the data using the ContentResolver:

getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

This is done for all RawContacts IDs that have been found previously. The effect is, of course, that all data is added repeatedly. Thus I want to return only one result now, but this has to meet special requirements.

I would like to adjust my function above so that its result meets the following requirements:

  1. ContactsContract.RawContactsColumn.DELETED must be 0
  2. The RawContacts entry must not be a secured one like Facebook’s
  3. ContactsContract.SyncColumns.ACCOUNT_TYPE is preferably “com.google”. So if there is one entry that meets this requirement, it should be returned. If there is none, return any of the remaining entries.

How can I do this (most efficiently)? I don’t want to make the query to complex.

  • 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-06-18T22:29:42+00:00Added an answer on June 18, 2026 at 10:29 pm

    I have given this some thought, from my experience with contact r/w, and with your needs in mind. I hope this helps you solve the issue and or points you in the direction you are looking for.
    Please note that i have no device available with any sync adapters such as facebook so unfortunately i cannot confirm my answer viability (the read only bit mainly which might changeable to a simple != ” ).

    Same getRawContactID function with some adjustments

    protected ArrayList<Long> getRawContactID(String contact_id) {
        HashMap<String,Long> rawContactIDs = new HashMap<String,Long>();
        String[] projection = new String[] { ContactsContract.RawContacts._ID, ContactsContract.RawContacts.ACCOUNT_TYPE };
        String where = ContactsContract.RawContacts.CONTACT_ID + " = ? AND " + ContactsContract.RawContacts.DELETED + " != 1 AND " + ContactsContract.RawContacts.RAW_CONTACT_IS_READ_ONLY + " != 1" ;
        String[] selection = new String[] { contact_id };
        Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, where, selection, null);
        try {
            while (c.moveToNext()) {
                rawContactIDs.put(c.getString(1),c.getLong(0));
            }
        }
        finally {
            c.close();
        }
        return getBestRawID(rawContactIDs);
    }
    

    And another getBestRawID function to find the best suited account –

    protected ArrayList<Long> getBestRawID(Map<String,Long> rawContactIDs)
    {
        ArrayList<Long> out = new ArrayList<Long>();
        for (String key : rawContactIDs.KeySet())
        {
           if (key.equals("com.google"))
           {
              out.clear(); // might be better to seperate handling of this to another function to prevent WW3.
              out.add(rawContactIDs.get(key));
              return out;
           } else {
              out.add(rawContactIDs.get(key));
           }
        }
        return out;
    }
    

    Also note – I wrote most of the code without running / testing it. Apologies in advance.

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

Sidebar

Related Questions

Whenever I want to get data from a plist file I use the following
Whenever I want to modify a winform from another thread, I need to use
Following code gets executed whenever I want to persist any entity. Things seems to
As a PHP developer, I always use the code below whenever I want to
I want code to run whenever I create a new object. For example, see
I want an adaptable database scheme. But still use a simple table data gateway
I am new in C#.I want to add rows in a GridView in runtime.
Whenever I want to add a comment to an indented line in vim, I
In my function, I am creating unique variables that I want to add to
Generally whenever i want to push in the changes to remote Git i follow

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.