Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>By seeing the answers, I think you got the answer how to fetch contacts and now you want to get the selected contacts on your activity. </p> <blockquote> <p>To fetch contact number specific to the contact name:</p> </blockquote> <pre><code>ContentResolver contactResolver = getContentResolver(); Cursor cursor = contactResolver.query(Phone.CONTENT_URI, null, Phone.DISPLAY_NAME + "=?", new String[]{contactName}, null); if(cursor.getCount() &gt; 0){ cursor.moveToFirst(); do { String number = cursor.getString(mCursor.getColumnIndex(Phone.NUMBER)); }while (cursor.moveToNext() ); } </code></pre> <p><strong>Note:</strong> Here <code>contactName</code> is the contact name of which you want to fecth contact numbers.</p> <blockquote> <p>I am assuming that you have shown the contacts with checkbox in ListView and here is your solution to get the contact list selected by the user to your activity:</p> </blockquote> <p><strong>1.</strong> Start your contact activity with <code>startActivityForResult()</code>.</p> <p><strong>2.</strong> Initialize <code>ArrayList</code> variable in contact activity say it <code>contactArrayList</code>.</p> <p><strong>3.</strong> When user <code>checks the checkbox</code>, add this contact in your <code>contactArrayList</code> and keep on adding and when <code>unchecks</code> then remove the contact from the <code>contactArrayList</code>.</p> <p><strong>4.</strong> When user presses done then set the result ok with the selected contact list which you have added in <code>contactArrayList</code> like this:</p> <pre><code>Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putStringArrayList("contacts", contactArrayList); intent.putExtras(bundle); setResult(RESULT_OK, intent); </code></pre> <p>and <code>finish()</code> this activity.</p> <p><strong>5.</strong> On your calling activity override:</p> <pre><code>@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK &amp;&amp; data != null ){ Bundle bundle = new Bundle(); bundle = data.getExtras(); ArrayList list = bundle.getStringArrayList("contacts"); } } </code></pre> <p><strong>Note:</strong> The above code was tested over 2.3.3.</p>
 

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