开发者

Try to create multiple contacts programmatically with same firstname

开发者 https://www.devze.com 2023-04-04 13:24 出处:网络
I use following code for creating new contact programmatically. This code create a new contact in contact list with given details, but if firstname of contact same and run this code from loop for crea

I use following code for creating new contact programmatically. This code create a new contact in contact list with given details, but if firstname of contact same and run this code from loop for creating multiple contacts having same firstname fields e.g. "Crashre1234" then it add all fields in the single contact multiple time(according to loop). I use the current system time with fields to create difference. But no luck. I don't understand what I miss.

                ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
                op_list.clear();
                long tti = System.currentTimeMillis();
                ///for(int i=0;i<2;i++)
                {
                    try{

                        int backRefIndex = 0;
                        System.out.println("Array List: "+backRefIndex);
                        op_list.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                        .withValue(RawContacts.ACCOUNT_TYPE, null)
                        .withValue(RawContacts.ACCOUNT_NAME, null)
                        .build());
                        //backRefIndex = backRefIndex+1;

                        op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.GIVEN_NAME, "Crashre1234"+"\r")
                        .build());
                        //backRefIndex = backRefIndex+1;

                        /*op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.FAMILY_NAME, "ABCre12")
                        .build());*/
                        //backRefIndex = backRefIndex+1;

                         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                        .withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
                        .withValue(StructuredPostal.FORMATTED_ADDRESS, "Secret password"+tti)
                        .build());
                        // backRefIndex = backRefIndex+1;

                         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                        .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                        .withValue(Phone.NUMBER, 11+tti)
                        .withValue(Phone.TYPE, Phone.TYPE_HOME)
                        .withValue(Phone.LABEL, "")
                        .build());
                        // backRefIndex = backRefIndex+1;

                         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                         .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
                         .withValue(Email.DATA, "Test@11test.com12"+tti)
                         .withValue(Email.TYPE, Email.TYPE_HOME)
                         .build());
                        // backRefIndex = backRefIndex+1;
                         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                         .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
                         .withValue(Email.DATA, "mobile@test12.com12"+tti)
                         .withValue(Email.TYPE, Email.TYPE_HOME)
             开发者_JS百科            .build());
                        // backRefIndex = backRefIndex+1;

                         ContentProviderResult[] result = activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
                         Uri uri = result[0].uri;
                         System.out.println("URI: "+uri);
                         System.out.println("Thread finish");
                          }catch(OperationApplicationException exp){
                           exp.printStackTrace();
                          }catch(RemoteException exp){
                           exp.printStackTrace();
                        }
                    }   


Just move your oplist declaration and initialisation inside, the for block.

long tti = System.currentTimeMillis();
for(int i=0;i<2;i++)
{
    ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
    op_list.clear();
    try{

        int backRefIndex = 0;
        System.out.println("Array List: "+backRefIndex);
        op_list.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
        .withValue(RawContacts.ACCOUNT_TYPE, null)
        .withValue(RawContacts.ACCOUNT_NAME, null)
        .build());
        //backRefIndex = backRefIndex+1;

        op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
        .withValue(StructuredName.GIVEN_NAME, "Crashre1234"+"\r")
        .build());
        //backRefIndex = backRefIndex+1;

        /*op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
        .withValue(StructuredName.FAMILY_NAME, "ABCre12")
        .build());*/
        //backRefIndex = backRefIndex+1;

         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
        .withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
        .withValue(StructuredPostal.FORMATTED_ADDRESS, "Secret password"+tti)
        .build());
        // backRefIndex = backRefIndex+1;

         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
        .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
        .withValue(Phone.NUMBER, 11+tti)
        .withValue(Phone.TYPE, Phone.TYPE_HOME)
        .withValue(Phone.LABEL, "")
        .build());
        // backRefIndex = backRefIndex+1;

         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
         .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
         .withValue(Email.DATA, "Test@11test.com12"+tti)
         .withValue(Email.TYPE, Email.TYPE_HOME)
         .build());
        // backRefIndex = backRefIndex+1;
         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
         .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
         .withValue(Email.DATA, "mobile@test12.com12"+tti)
         .withValue(Email.TYPE, Email.TYPE_HOME)
         .build());
        // backRefIndex = backRefIndex+1;

         ContentProviderResult[] result = activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
         Uri uri = result[0].uri;
         System.out.println("URI: "+uri);
         System.out.println("Thread finish");
          }catch(OperationApplicationException exp){
           exp.printStackTrace();
          }catch(RemoteException exp){
           exp.printStackTrace();
    }
}   
0

精彩评论

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

关注公众号