0

I'm having a BIG touble when adding duplicate contact fields in Android 2.1 update 1

please have a look at my code:

      ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();

  op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
         .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
         .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
         .build());

  // first and last names
       op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
   .withValueBackReference(Data.RAW_CONTACT_ID, 0)
         .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
         .withValue(StructuredName.GIVEN_NAME, "MyFirstName")
         .withValue(StructuredName.FAMILY_NAME, "MyLastName")
         .build());

  try{
   ContentProviderResult[] results = cResolver.applyBatch(ContactsContract.AUTHORITY, op_list);
  }catch(Exception e){
   e.printStackTrace();
  }

Try to run this piece of code in a 20 iteration loop, then go to the contacts application you will see only 8 contacts hanging there!! This problem happens when also when I insert duplicate emails, phones, organizations. Try it in a loop from 0->200, android will go crazy!

is there a problem in my code? is there any solution to this?

any help will be really appreciated ... Thanks!

Shatazone
  • 2,422
  • 6
  • 28
  • 38

3 Answers3

9

After 2 non sleep days... rereading my code and rewriting my classes I found the solution to this: there is a column that I was not aware of, and it didn't even exist in earlier APIs called: AGGREGATION_MODE

so the solution turned out to be this:

    ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
         op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
             .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
             .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
             .withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DISABLED)

             .build());

      // first and last names
           op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
       .withValueBackReference(Data.RAW_CONTACT_ID, 0)
             .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
             .withValue(StructuredName.GIVEN_NAME, "MyFirstName")
             .withValue(StructuredName.FAMILY_NAME, "MyLastName")
             .build());

      try{
       ContentProviderResult[] results = cResolver.applyBatch(ContactsContract.AUTHORITY, op_list);
      }catch(Exception e){
       e.printStackTrace();
      }
Shatazone
  • 2,422
  • 6
  • 28
  • 38
1

The contacts are merged in new api (sdk >2.0).

All the contacts which fall into a specific set of rules are automatically merged to display a single contact in the contact list.

The set of rules include 1. Contacts having the same First name and Last Name. 2. Contacts having the same mail ID. 3. Contact having a name and a Phone number matching ...etc.

In the Display Contact details screen the duplicates are not visible. If you try to edit the contact you will find all the instances of the contact that you had entered. (i.e if a contact is entered 10 times then 10 different instances are visible one after the other.) You can edit any particular contact there.

As Android 2.0 supports multiple Accounts this scenario would exists.

Manish Khot
  • 3,027
  • 2
  • 29
  • 37
0

You should try with direct insertion with a ContentResolver , maybe it would help (I haven't tried to insert 20 times the same contact, so I don't know if it will make any difference)

apps
  • 942
  • 5
  • 8
  • strange... explain this: when you try with the 20s and you get your 8s, if you run the app again can you add again a few records or not ? – apps Nov 14 '10 at 02:23
  • yes i can ... if i add 10 different new contacts, all of them are added... but if i try to add more duplicates, small number of them is added :S! – Shatazone Nov 14 '10 at 02:31
  • well, you can add some more the next time, so it sounds like that the contacts api just can't insert that many records at once. You can test it with trying to add 20 records in a loop that are different, which will show if the problem is related to the same contacts thing or to the multiple contacts thing. But so far it looks like the second, so you can add them in portions, 5-10 at a time :) – apps Nov 14 '10 at 02:37
  • and btw, the contacts API is different in 2.0, just to let you know if you don't know it of course, it is with the ContactsContract – apps Nov 14 '10 at 02:38
  • well yeah of course I am aware about the api differences, been working on that for last month hehe ... umm well i tried earlier adding different contacts (720 different contacts) full of fields (organizations, phones and even some have a photo) and they were all added successfully – Shatazone Nov 14 '10 at 02:44
  • Ok, the 720 contacts at once part sounds interesting, do you see any strange things in the log, sometimes there isn't any crash but an error in the native code happens. I will not ask you if you insert 20 times 1 record or 1 time 20 records (which is the good way) because I am assuming you are doing it that way – apps Nov 14 '10 at 02:48
  • :D I FOUND THE SOLUTION TO THIS! – Shatazone Nov 14 '10 at 13:04