1

I am trying to add new numbers (or emails or websites) to an existing contact, but the code does not work well. The code is as followings:

int rowId = cursor1.getInt(cursor1
                .getColumnIndex(ContactsContract.RawContacts._ID));
ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rowId);
contentValues.put(ContactsContract.Data.MIMETYPE,
        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER,
        "45435345");
contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE,
        Phone.TYPE_HOME);

ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
        .withValues(contentValues).build());

when the codes run ,there is no errors,and there is no changes.I am depressed with it.Any help will save me!!!

KingBright
  • 11
  • 3

2 Answers2

0

use this code to insert New Contatct in phone Contatcs:

Intent intent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, Uri.parse("tel:" + currentNum.getText())); //currentNum is my TextView, you can replace it with the number directly such as Uri.parse("tel:1293827")
intent.putExtra(ContactsContract.Intents.EXTRA_FORCE_CREATE, true); //skips the dialog box that asks the user to confirm creation of contacts
startActivity(intent);

enjoy your code:)-

John smith
  • 1,781
  • 17
  • 27
0

Maybe you need these two permissions:

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

Perhaps you need to use ContentResolver instead of ContentValues.

A. Abiri
  • 10,750
  • 4
  • 30
  • 31
  • I have already solved my problem,but thank u for your answer anyway.I think the problem is caused by my wrong codes,which destroyed the structrue of the data in database.When I create a new emulator, the codes works well.^_^ – KingBright Jul 25 '11 at 07:33