【问题标题】:Details not coming properly by using Intent?使用 Intent 无法正确获取详细信息?
【发布时间】:2012-06-14 19:46:48
【问题描述】:

使用此代码无法正确显示联系人的详细信息。我正在尝试使用 Intent 访问 姓名、号码和电子邮件 ID。名称和编号仅在我单击按钮时显示,而不是电子邮件。项目中没有错误。它工作正常。我的 xml 文件只有一个按钮。

公共类 GetDetails 扩展 Activity {

/** Called when the activity is first created. */
private static final int CONTACT_PICKER_RESULT = 1001; 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

       Button Btn = (Button)findViewById(R.id.getContacts);
        Btn.setOnClickListener(new View.OnClickListener() { 

            @Override 
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(i, CONTACT_PICKER_RESULT);

            } 
        });
    }

    protected void onActivityResult(int reqCode, int resultCode, Intent data) { 
        super.onActivityResult(reqCode, resultCode, data);
        if(resultCode == RESULT_OK) {
            switch (reqCode) {
            case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                Cursor emails = null;
                String number = "";
                String emailID = "";
                try {

                    Uri result = data.getData();

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

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

                    int numberIdx = cursor.getColumnIndex(Phone.DATA);  
                     if(cursor.moveToFirst()) {
                        number = cursor.getString(numberIdx);

                    } else {

                    }
                      emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
                     int num = emails.getColumnIndex(Email.DATA);
                     if(emails.moveToFirst()) {
                         emailID = emails.getString(num);

                     } else { 

                     }

                } catch (Exception e) {
                    //failed
                } finally {
                    if (cursor!=null) {
                        cursor.close();
                    }


                }

            }
        }
    }
}

如何解决这种情况?

【问题讨论】:

  • stackoverflow.com/questions/11032030/…我在这里回答了这个问题
  • 你的回答会找回姓名号码和邮箱ID吗?
  • 但它显示了一些错误,例如“无法解决”,其中哪些是变量。适配器、setReminder、mcontext、numbers、chooseContactArray 上的错误...我创建了一些变量,除此之外它会增加错误。

标签: android


【解决方案1】:

替换这一行:

emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);

这一行:

emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = ?", new String[]{id}, null);

【讨论】:

  • 阿伦,没有任何改变吗?
  • 你能看到名字和号码吗?
  • 当然我会得到姓名和号码,在我尝试获取电子邮件之后,当时也不会显示任何内容。我认为查询几乎没有错误。
  • 一个小问题...您确定您尝试访问的联系人在您的设备中注册了电子邮件地址吗?
  • 实际上我在模拟器设备中有 7 个联系人,除了 1 个联系人之外,所有联系人都有号码和电子邮件 ID。姓名对所有 7 人都是强制性的。对于那 1 个联系人,我只保存了姓名和电子邮件 ID,但它不会显示在屏幕上。所有其他人都在显示屏上
猜你喜欢
  • 2012-12-23
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 2022-10-02
  • 2015-11-18
  • 2019-02-18
  • 2016-07-11
  • 1970-01-01
相关资源
最近更新 更多