开发者

Android not adding all contacts with duplicate fields

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
 开发者_StackOverflow社区      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!


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();
      }


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜