尝试以编程方式创建多个联系人具有相同的名字 [英] Try to create multiple contacts programmatically with same firstname

查看:156
本文介绍了尝试以编程方式创建多个联系人具有相同的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code的编程方式创建新的联系人。 这code创建联系人列表的详情,请一个新的联系人,但是,如果接触的名字相同,运行循环这个$ C $下创建具有相同名字的字段,例如多个联系人Crashre1234,那么它​​(根据循环)添加的所有领域中的单个联系人的多个时间。我用的是当前系统时间与领域创造差异。 但没有运气。 我不明白我错过。

 的ArrayList< ContentProviderOperation> op_list =新的ArrayList< ContentProviderOperation>();
                op_list.clear();
                长TTI = System.currentTimeMillis的();
                ///的for(int i = 0;我2;我++)
                {
                    尝试{

                        INT backRefIndex = 0;
                        的System.out.println(数组列表:+ backRefIndex);
                        op_list.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                        .withValue(RawContacts.ACCOUNT_TYPE,空)
                        .withValue(RawContacts.ACCOUNT_NAME,空)
                        。建立());
                        // 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)
                        。建立());
                        // 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)
                        。建立());*/
                        // 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,秘密口令+ TTI)
                        。建立());
                        // 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,)
                        。建立());
                        // 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)
                         。建立());
                        // 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)
                         。建立());
                        // backRefIndex = backRefIndex + 1;

                         。ContentProviderResult []结果= activity.getContentResolver()applyBatch(ContactsContract.AUTHORITY,op_list);
                         开放的我们的uri =结果[0] .uri;
                         的System.out.println(URI:+ URI);
                         的System.out.println(线程结束);
                          }赶上(OperationApplicationException EXP){
                           exp.printStackTrace();
                          }赶上(RemoteException的EXP){
                           exp.printStackTrace();
                        }
                    }
 

解决方案

刚刚移动你的OPLIST声明和初始化里面,块。

 长TTI = System.currentTimeMillis的();
的for(int i = 0;我2;我++)
{
    ArrayList的< ContentProviderOperation> op_list =新的ArrayList< ContentProviderOperation>();
    op_list.clear();
    尝试{

        INT backRefIndex = 0;
        的System.out.println(数组列表:+ backRefIndex);
        op_list.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
        .withValue(RawContacts.ACCOUNT_TYPE,空)
        .withValue(RawContacts.ACCOUNT_NAME,空)
        。建立());
        // 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)
        。建立());
        // 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)
        。建立());*/
        // 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,秘密口令+ TTI)
        。建立());
        // 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,)
        。建立());
        // 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)
         。建立());
        // 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)
         。建立());
        // backRefIndex = backRefIndex + 1;

         。ContentProviderResult []结果= activity.getContentResolver()applyBatch(ContactsContract.AUTHORITY,op_list);
         开放的我们的uri =结果[0] .uri;
         的System.out.println(URI:+ URI);
         的System.out.println(线程结束);
          }赶上(OperationApplicationException EXP){
           exp.printStackTrace();
          }赶上(RemoteException的EXP){
           exp.printStackTrace();
    }
}
 

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

这篇关于尝试以编程方式创建多个联系人具有相同的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆