Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a sample bit of code to display all email addresses in the contact list to the user and allow them to select a single one (which is then put into an <code>EditText</code> with the id of <em>R.id.youredittextid</em>).</p> <p><em><strong>Note:</em></strong> This is a rather inefficient way to do this, and will cause quite a delay if you have lots of contacts. But all the necessary code is here; customize as you see fit...</p> <pre><code> // We're going to make up an array of email addresses final ArrayList&lt;HashMap&lt;String, String&gt;&gt; addresses = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); // Step through every contact final ContentResolver cr = getContentResolver(); final Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { final String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); final String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // Pull out every email address for this particular contact final Cursor emails = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); while (emails.moveToNext()) { // Add email address to our array final String strEmail = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); final HashMap&lt;String, String&gt; email = new HashMap&lt;String, String&gt;(); email.put("Name", contactName); email.put("Email", strEmail); addresses.add(email); } emails.close(); } // Make an adapter to display the list SimpleAdapter adapter = new SimpleAdapter(this, addresses, android.R.layout.two_line_list_item, new String[] { "Name", "Email" }, new int[] { android.R.id.text1, android.R.id.text2 }); // Show the list and let the user pick an email address new AlertDialog.Builder(this) .setTitle("Select Recipient") .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { EditText e = (EditText)findViewById(R.id.youredittextid); HashMap&lt;String, String&gt; email = addresses.get(which); e.setText(email.get("Email")); dialog.dismiss(); } }).create().show(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload