Trying to insert contact into edittext using contact picker

心已入冬 提交于 2021-01-28 18:42:23

问题


I'm trying to allows user to insert their number using a contact picker in android. I am currently using the example from post 2 in Getting Number from Contacts Picker the contact picker appears and so but when i select a contact the contact number doesn't affect inside my edittext. There's no logcat error or whatsoever.

My code:

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
    }  
}

回答1:


This below code is one I am using and it is working pretty fine for me. Try this one.

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

            }

Create your edittext as a class variable.. so you can also apply a empty check, I am very much sure you are creating it again for that purpose..



来源:https://stackoverflow.com/questions/11781618/trying-to-insert-contact-into-edittext-using-contact-picker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!