title: How to open contacts and show the picked contacts in a list?
body: <p>I am trying to make a button to opens up my contacts and let me choose contacts I want to pick. Then, list the names and(maybe) their mobile numbers on a list. So far, I'm stuck on opening the contacts from my phone. Hope you can help me. Thanks! Here's my code:</p>
  
  <pre><code>public class RecipientsFragment extends Fragment {
  public Button button;
  static final int PICK_CONTACT=1;
  
  public RecipientsFragment() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_recipients, container, false);
      button = (Button) view.findViewById(R.id.button);
  
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
              startActivityForResult(intent, PICK_CONTACT);
  
          }
      });
      return inflater.inflate(R.layout.fragment_recipients, container, false);
  }
  @Override
  public void onActivityResult(int reqCode, int resultCode, Intent data) {
      super.onActivityResult(reqCode, resultCode, data);
  
      switch (reqCode) {
          case (PICK_CONTACT) :
              if (resultCode == Activity.RESULT_OK) {
                  String cNumber = null;
                  Uri contactData = data.getData();
                  Cursor c =  getActivity().managedQuery(contactData, null, null, null, null);
                  if (c.moveToFirst()) {
  
  
                      String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
  
  
                      String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
  
                      if (hasPhone.equalsIgnoreCase("1")) {
                          Cursor phones = getActivity().getContentResolver().query(
                                  ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
                                  null, null);
                          phones.moveToFirst();
                          cNumber = phones.getString(phones.getColumnIndex("data1"));
  
                      }
                      String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                      System.out.println("number is:"+cNumber+" Name :"+name);
  
                  }
              }
              break;
      }
  }
  
  
  
  }
  </code></pre>