Answer the question
In order to leave comments, you need to log in
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);
}
});
@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);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question