Guest User

Untitled

a guest
Aug 14th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. How do i get firstname and surname from a contact picker
  2. Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
  3. startActivityForResult(intent, enumRequestCode.PICK_CONTACT.ordinal());
  4.  
  5. @Override
  6. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  7.  
  8. if(enumRequestCode.PICK_CONTACT.ordinal() == requestCode){
  9. if (resultCode == Activity.RESULT_OK) {
  10. Cursor cursor = managedQuery(data.getData(), null, null, null, null);
  11. while (cursor.moveToNext())
  12. {
  13. String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
  14. String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
  15.  
  16. String phoneNumber = "";
  17. int hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
  18.  
  19.  
  20. String firstName = "";
  21. String surName = "";
  22.  
  23.  
  24. if (hasPhone >= 1){
  25. Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
  26. phones.moveToNext();
  27. phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  28.  
  29.  
  30.  
  31. phones.close();
  32. }
  33.  
  34. // String firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
  35. // String surName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
  36.  
  37. Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
  38. while (cursor.moveToNext()) {
  39. String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
  40. name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
  41.  
  42. String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
  43.  
  44. if (hasPhone.equalsIgnoreCase("1"))
  45. hasPhone = "true";
  46. else
  47. hasPhone = "false" ;
  48.  
  49. if (Boolean.parseBoolean(hasPhone)) {
  50. Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
  51. if(phones.getCount() > 1) {
  52. ArrayList<String> p = new ArrayList<String>();
  53.  
  54. while (phones.moveToNext()) {
  55. p.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
  56. }
  57. final CharSequence[] items = p.toArray(new CharSequence[p.size()]);
  58. AlertDialog.Builder builder = new AlertDialog.Builder(newContact.this);
  59. builder.setItems(items, new DialogInterface.OnClickListener() {
  60. public void onClick(DialogInterface dialog, int item) {
  61. phoneNumber = (String) items[item];
  62. showContentSettings();
  63. }
  64. });
  65. AlertDialog alert = builder.create();
  66. alert.show();
  67. }else {
  68. phones.moveToFirst();
  69. phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  70. showContentSettings();
  71. }
  72. phones.close();
  73. }else {
  74. showDialog(DIALOG_PHONE_NOT_FOUND);
  75. }
  76. }
  77. cursor.close();
  78. Log.d(TAG,"name: " + name + "nphoneNumber: " + phoneNumber);
Add Comment
Please, Sign In to add comment