【问题标题】:Contacts aren't showing up on my edittext联系人未显示在我的编辑文本中
【发布时间】:2017-05-22 06:39:18
【问题描述】:

所以我一直在尝试将联系人添加到编辑文本中,我使用 Onclickevent 来调用联系人,然后一旦选择了联系人,就应该将其写入编辑文本,但我无法做到这一点,下面是我的 Onactivity 结果,

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                String name = "";
                try {
                    Uri result = data.getData();
                    //writeToFile("uri" +result);
                    String id = result.getLastPathSegment();

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

                    if (cursor != null && cursor.moveToFirst())
                    {
                        int phoneIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA);
                        int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                        writeToFile("ifcursor" +phoneIdx+nameIdx);
                        name = cursor.getString(nameIdx);
                    }



                } catch (Exception e) {
                    //Log.e(DEBUG_TAG, "Failed to get name", e);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                   // phNo = (EditText) findViewById(R.id.phone_number);
                    phNo.setText(name);
                    if (name.length() == 0) {
                      Toast.makeText(getApplicationContext(),"Name not found for contact.",Toast.LENGTH_LONG).show();
                    }

                }

                break;
        }

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

任何帮助将不胜感激,它停留在“找不到名称”上

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    试试这个

      @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // Check which request it is that we're responding to
            if (requestCode == PICK_CONTACT_REQUEST) {
                // Make sure the request was successful
                if (resultCode == RESULT_OK) {
                    // Get the URI that points to the selected contact
                    Uri contactUri = data.getData();
                    // We only need the NUMBER column, because there will be only one row in the result
                    String[] projection = {Phone.NUMBER};
    
                    // Perform the query on the contact to get the NUMBER column
                    // We don't need a selection or sort order (there's only one result for the given URI)
                    // CAUTION: The query() method should be called from a separate thread to avoid blocking
                    // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
                    // Consider using CursorLoader to perform the query.
                    Cursor cursor = getContentResolver()
                            .query(contactUri, projection, null, null, null);
                    cursor.moveToFirst();
    
                    // Retrieve the phone number from the NUMBER column
                    int column = cursor.getColumnIndex(Phone.DISPLAY_NAME);
                    String number = cursor.getString(column);
                      phNo.setText(number );
    
    
                    // Do something with the phone number...
                }
            }
        }
    

    【讨论】:

    • 它说传递结果失败,列数据无效1
    • @jaikhambhayta 您无法从联系人表中查询电话号码,查询必须在CommonDataKinds.Phone.CONTENT_URI 表中
    【解决方案2】:

    如果您需要用户使用电话号码选择联系人,您应该调用以下意图:

    Intent i = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);
    startActivityForResult(i, CONTACT_PICKER_RESULT);
    

    然后你可以像这样处理onActivityResult中的响应:

    Uri result = data.getData();
    // because we asked for a Phone.CONTENT_URI picker, we'll get a uri for a Phone._ID entry
    String id = result.getLastPathSegment(); 
    String[] projection = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER };
    String selection = Phone._ID + "=" + id;
    Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, projection, selection,null, null);
    
    if (cursor != null && cursor.moveToFirst()) {
        String name = cursor.getString(0);
        String number = cursor.getString(1);
        Log.d("MyActivity", "got name=" + name + ", phone=" + number);
    }
    if (cursor != null) {
        cursor.close();
    }
    

    【讨论】:

    • contacts.contracts.phone 和 just phone 有什么区别,因为 phone 似乎被贬低了
    • 确保从ContactsContract.CommonDataKinds.Phone 导入Phone,我猜它会自动导入错误的类
    • 这得到了联系人,但不是我选择的那个,我在 if(){cursor.close()} 之后使用了 ph.edittext,我做错了什么吗??
    • hmmm...这很奇怪,您得到的是姓名+号码,但联系方式有误?你试过几次了吗?你总是得到同一个错误的联系人,还是随机的?
    • 同一个错误的联系人,但没关系我尝试了不同的方式来实现它,你知道我猜,非常感谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多