单击动态edittext,在android中为活动结果设置特定edittext的联系号码

时间:2015-03-17 16:58:36

标签: android android-contacts

单击动态编辑文本时,我尝试在编辑文本中设置联系人号码。我收到了交换机案例错误。

这是动态编辑文本,我实现onclickListener会转到联系意图。

if ((list.getCode().equalsIgnoreCase("XXXXX"))) {
                    editText[editCounter].setOnClickListener(this);

                    editText[editCounter].setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                            startActivityForResult(intent, PICK_CONTACT);
                        }
                    });

//这是onactivty结果代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
        case PICK_CONTACT:

            Cursor cursor = null;  
            String phoneNumber = "";
            List<String> allNumbers = new ArrayList<String>();
            int phoneIdx = 0;
            try {  
                Uri result = data.getData();  
                String id = result.getLastPathSegment();  
                cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  
                phoneIdx = cursor.getColumnIndex(Phone.DATA);
                if (cursor.moveToFirst()) {
                    while (cursor.isAfterLast() == false) {
                        phoneNumber = cursor.getString(phoneIdx);
                        allNumbers.add(phoneNumber);
                        cursor.moveToNext();
                    }
                } else {
                    //no results actions
                }  
            } catch (Exception e) {  
               //error actions
            } finally {  
                if (cursor != null) {  
                    cursor.close();

                }


                final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]);
                AlertDialog.Builder builder = new AlertDialog.Builder(CustomFormActivity.this);
                builder.setTitle("Choose a number");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        String selectedNumber = items[item].toString();
                        selectedNumber = selectedNumber.replace("-", "");




                       /* for (ControlListObject list : controlArray) {

                             if((list.getCode().equalsIgnoreCase("XXXXX")))
                                    {
                        editText[editCounter].setText(selectedNumber);}
                        }*/
                    }
                });
                AlertDialog alert = builder.create();
                if(allNumbers.size() >= 1) {
                    alert.show();
                } else {
                    String selectedNumber = phoneNumber.toString();
                    selectedNumber = selectedNumber.replace("-", "");

                   // editText.setText(selectedNumber);

                    for (ControlListObject list : controlArray) {


                         if((list.getCode().equalsIgnoreCase("XXXXX")))
                                {


                   editText[editCounter].setText(selectedNumber);


                                }}
                }
                        // }




                if (phoneNumber.length() == 0) {  
                    //no numbers found actions  
                }  
            }  
            break;  
        }  
    } else {
       //activity result error actions
    }  
}

0 个答案:

没有答案