1

我试图允许用户使用 android 中的联系人选择器插入他们的号码。我目前正在使用从联系人选择器获取号码中的帖子 2 中的示例,联系人选择器会出现,但是当我选择一个联系人时,联系人号码不会影响我的编辑文本。没有logcat错误或任何东西。

我的代码:

public void doLaunchContactPicker(View view) {  
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
            Contacts.CONTENT_URI);  
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);  
}  

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (resultCode == RESULT_OK) {  

            switch (requestCode) {  
        case CONTACT_PICKER_RESULT:
            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 EditText phoneInput = (EditText) findViewById(R.id.mobileno);

                final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]);
                AlertDialog.Builder builder = new AlertDialog.Builder(SIMMessageSenderActivity.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("-", "");
                        phoneInput.setText(selectedNumber);
                    }
                });
                AlertDialog alert = builder.create();
                if(allNumbers.size() > 1) {
                    alert.show();
                } else {
                    String selectedNumber = phoneNumber.toString();
                    selectedNumber = selectedNumber.replace("-", "");

                    phoneInput.setText(selectedNumber);
                }

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

1 回答 1

2

下面的代码是我正在使用的代码,它对我来说工作得很好。试试这个。

  if((requestCode == PICK_CONTACT) && (resultCode == RESULT_OK))
            { 
                 if (data != null) {
                     Uri contactData = data.getData();

                     try {

                         String id = contactData.getLastPathSegment();
                        String[] columns = {Phone.DATA,Phone.DISPLAY_NAME};
                         Cursor phoneCur = getContentResolver()
                                 .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        columns ,
                                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                 + " = ?", new String[] { id },
                                         null);

                         final ArrayList<String> phonesList = new ArrayList<String>();
                         String Name = null ;
                         if(phoneCur.moveToFirst())
                         {
                             do{
                                 Name = phoneCur.getString(phoneCur.getColumnIndex(Phone.DISPLAY_NAME));
                                 String phone = phoneCur
                                 .getString(phoneCur
                                         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                                     phonesList.add(phone);

                               }   while (phoneCur.moveToNext());

                         }


                         phoneCur.close();

                         if (phonesList.size() == 0) {
                             Toast.makeText(
                                     this,"This contact does not contacin any number",
                                     Toast.LENGTH_LONG).show();
                         } else if (phonesList.size() == 1) {
                             toET.setText(phonesList.get(0));
                         } else {

                             final String[] phonesArr = new String[phonesList
                                     .size()];
                             for (int i = 0; i < phonesList.size(); i++) {
                                 phonesArr[i] = phonesList.get(i);
                             }

                             AlertDialog.Builder dialog = new AlertDialog.Builder(
                                     MessageManagerActivity.this);
                             dialog.setTitle("Name : "+Name);
                             ((Builder) dialog).setItems(phonesArr,
                                     new DialogInterface.OnClickListener() {
                                         public void onClick(
                                                 DialogInterface dialog,
                                                 int which) {
                                             String selectedEmail = phonesArr[which];
                                             toET.setText(selectedEmail);
                                         }
                                     }).create();
                             dialog.show();
                         }
                     } catch (Exception e) {
                         Log.e("FILES", "Failed to get phone data", e);
                     }
                 }

            }

将您的edittext创建为类变量..因此您也可以应用空检查,我非常确定您为此目的再次创建它..

于 2012-08-02T16:33:17.407 回答