38526

Android : Adding Notes to Existing Contacts

I'm trying to use the ContactsContract api to add some notes to my contacts. I'm not sure I fully understand the various contact IDs and Raw Contact IDs. My problem seems pretty similar to what is discussed here.

I want to : 1. Find a specific contact. 2. When found, insert specific notes

I'm doing it in the following way:

Cursor contacts = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null, null, null); while(contacts.moveToNext()){ Log.d("TC", "Found : " + name); int rid = contacts.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID); int rawContactID = contacts.getInt(rid); int nameIdx = contacts.getColumnIndex(ContactsContract.Data.DISPLAY_NAME); String name = contacts.getString(nameIdx); if <Some condition>{ ContentValues contentValues = new ContentValues(); contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactID); contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE); contentValues.put(ContactsContract.CommonDataKinds.Note.NOTE, "Bazzinga !"); int stat = getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, contentValues, null , null); Log.d("TC", "Update returned : " + stat); } }

I get no errors. But the contact doesn't get updated :(

Answer1:

Replace thegetContentResolver().update(...)with

getContentResolver().insert( Data.CONTENT_URI, contentValues );

Also you should have a where statement in the Data.CONTENT_URI query. Because Data table can have multiple entries with same raw_contact_id. You can use "Data.MIMETYPE = StructuredName.CONTENT_ITEM_TYPE".

Recommend

  • How to zip multiple Boost Preprocessor sequences?
  • Migrating a Google App Engine application from Django 0.96 to Django 1.2
  • Resharper : subtituate macro with multi-line code
  • Calling a Java method from a sqlite trigger (android)
  • Error casting Object[] to ContentValues[]
  • Delete contact from android contacts
  • Clear chrome browser history programmatically
  • Get VS to highlight all .Net 3.5 SP1 method calls
  • Get file path from Uri from Video Chooser
  • Android CalendarContract, deleting a recurring event causes all events to disappear on calendar?
  • TelephonyManager crashing on android studio
  • d3js: time scaling and “1901”
  • Aspect advising other aspects
  • Can't get LogCat (Alcatel OneTouch Evolve)
  • substitute period from abbreviation (single letter + period) unless followed by a capital letter
  • Defining variable by logical subseting on time interval in data.table
  • How to force refresh on CallLog.Calls.CACHED_NAME column?
  • Query timeout expired in django-mssql when executing custom SQL directly
  • Visual studio alerts workspace already exists
  • Partial specialization of a class template in derived class affects base class
  • Pre-populated SQLite Database not reading properly in Android Studio
  • Converting query results into DataFrame in python
  • Possible to get mouse events fired when cursor is outside page?
  • x64 applications using gdi+: what are the consequences on performance?
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • PHP - How to update data to MySQL when click a radio button
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • Display Images one by one with next and previous functionality
  • Matrix multiplication with MKL
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • SQL merge duplicate rows and join values that are different
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time