A
A
AndreyOstapov2018-12-28 19:44:14
Java
AndreyOstapov, 2018-12-28 19:44:14

How to get phone number from contact list?

Please tell me how to get a number by username from the contact list. I implemented the intent:

final Intent pickContact = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
mSuspectButton = v.findViewById(R.id.crime_suspect);
mSuspectButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivityForResult(pickContact, REQUEST_CONTACT);
    }
});

Then I get the username:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_DATE && resultCode == Activity.RESULT_OK){
        Date date = (Date) data.getSerializableExtra(DatePickerFragment.getExtraDate());
        mCrime.setDate(date);
        updateDate(mCrime.getDateString());
    }else if (requestCode == REQUEST_CONTACT && data != null){
        Uri contactUri = data.getData();
        //определение полей, значения которых должны быть возвращены запросом
        String[] queryFields = new String[] {Contacts.DISPLAY_NAME};
        try (Cursor cursor = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null)) {
            if (cursor.getCount() == 0) {
                return;
            }
            cursor.moveToFirst();
            String suspect = cursor.getString(0);
            mCrime.setSuspect(suspect);
            mSuspectButton.setText(suspect);
        }

    }
}

And now I need to get that user's phone number and store it in a separate variable. I'm newbie and don't understand how to do this. Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question