联系人不会显示在EditText中

时间:2011-12-09 17:34:29

标签: android eclipse android-edittext contacts

我有一个应用程序应该将联系人电话号码调用到指定的EditText视图中。我有一个调用联系人选择器的按钮,它工作正常,但当我点击它时,它不会将我想要的信息添加到视图中。

MY ONCLICKlISTENER

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

我的STARTACTIVITYFORRESULT

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

        if (resultCode == RESULT_OK) {  
            switch (requestCode) {  
            case GET_CONTACT:  
                Cursor cursor = null;  
                String Number = "";  
                try {  
                    Uri result = data.getData();  
                    Log.v(NOTIFICATION_SERVICE, "Got a contact result: "  
                            + result.toString());  

                    // get the contact id from the Uri  
                    String id = result.getLastPathSegment();  

                    // query for everything email  
                    cursor = getContentResolver().query(Phone.CONTENT_URI,  
                            null, Phone.CONTACT_ID + "=?", new String[] { id },  
                            null);  

                    int emailIdx = cursor.getColumnIndex(Phone.DATA);  

                    // let's just get the first email  
                    if (cursor.moveToFirst()) {  
                        Number = cursor.getString(emailIdx);  
                        Log.v(NOTIFICATION_SERVICE, "Got number: " + Number);  
                    } else {  
                        Log.w(NOTIFICATION_SERVICE, "No results");  
                    }  
                } catch (Exception e) {  
                    Log.e(NOTIFICATION_SERVICE, "Failed to get number data", e);  
                } finally {  
                    if (cursor != null) {  
                        cursor.close();  
                    }  
                     EditText mNumbers = (EditText)findViewById(R.id.editNumber);
                    mNumbers.setText(Number);  
                    if (Number.length() == 0) {  
                        Toast.makeText(this, "No number found for contact.",  
                                Toast.LENGTH_LONG).show();  
                    }  

                }  

                break;  
            }  

        } else {  
            Log.w(NOTIFICATION_SERVICE, "Warning: activity result not ok");  
        }  

 }  

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

确保添加

<uses-permission android:name="android.permission.READ_CONTACTS" />

到你的清单

答案 1 :(得分:0)

你没有在你的代码中使用任何Bundles。 Viz:Bundle extras = data.getExtras 如需完整参考,请查看网站:http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/