如何从文本视图到联系人列表依次显示和保存手机号码



我正在制作一个android应用程序,它将接受用户的前四位数字,不仅将从1-100开始按顺序显示数字,还将分别保存在联系人中。例如,用户类型9179和TextView将显示9179000000到9179000100。问题是我的代码将号码保存在一个联系人中,而不是作为个人号码。这是代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    test=(TextView)findViewById(R.id.textView1);
    getit=(EditText)findViewById(R.id.editText1);
    b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            StringBuilder t = new StringBuilder(); 
            String result=getit.getText().toString();
            for (int l=series; l<=100; l++) {
                t.append(result);
                t.append(String.format("%06d", l)+"n");
                }
            test.setText(t.toString());  
            String sphone=test.getText().toString();
        /*  String result=getit.getText().toString();
            test.setText(result);  */
            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
            int rawContactInsertIndex = ops.size();
            ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                    .withValue(RawContacts.ACCOUNT_TYPE, null)
                    .withValue(RawContacts.ACCOUNT_NAME, null).build());
            ops.add(ContentProviderOperation
                    .newInsert(Data.CONTENT_URI)
                    .withValueBackReference(
                            ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex)
                    .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                    .withValue(Phone.NUMBER, sphone) // Number of the person
                    .withValue(Phone.TYPE, Phone.TYPE_MOBILE).build()); // Type of mobile number                    
            try
            {
                ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            }
            catch (RemoteException e)
            { 
                // error
                e.printStackTrace();
            }
            catch (OperationApplicationException e) 
            {
                // error
                e.printStackTrace();
            }
        }
    });

}
}

我没有检查您的ContentProviderOperation代码,但如果它正确且有效,那么代码的其余部分应该如下所示。

在for循环之后,您只创建了1个String sphone。相反,for循环应该扩展到创建ContentProviderOperation对象。使用此功能和applyBatch功能,您可以同时添加多个联系人。

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            final String result = getit.getText().toString();
            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
            for (int l=series; l<=100; l++) {
                String sphone = result + String.format("%06d", l);
                int rawContactInsertIndex = ops.size();
                ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                        .withValue(RawContacts.ACCOUNT_TYPE, null)
                        .withValue(RawContacts.ACCOUNT_NAME, null).build());
                ops.add(ContentProviderOperation
                        .newInsert(Data.CONTENT_URI)
                        .withValueBackReference(
                                ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                        .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                        .withValue(Phone.NUMBER, sphone) // Number of the person
                        .withValue(Phone.TYPE, Phone.TYPE_MOBILE).build()); // Type of mobile number
            }
            try {
                ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (RemoteException e) {
            } catch (OperationApplicationException e) {
            }
        }

相关内容

最新更新