从联系人处获取电话号码

时间:2013-09-02 19:27:02

标签: android string contacts

我点击了一个按钮,显示了联系人。它显示名称,但不显示所选联系人的号码......

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c = managedQuery(contactData, null, null, null, null);
            if (c.moveToFirst()) {
                String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                contactName.setText(name);

                Something here !!!!!!!!!!!!!!!!!!!!!!!!

                contactName.setText(number);
            }
        }
    }
}

它将NAME串起来,但我该如何获取电话号码?

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contactName.setText(name);
            contactNumber.setText(number);
        }
    }

使用此代码可以打开我的联系人。我触摸一个,但它总是显示我添加到联系人的最新名称和号码。

例如,我触摸 Adrian(22222222)并显示Paul (33333333),这是我添加的最后一个人。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));