How to retrieve contact’s phone number (Android)?

I am having an absolute nightmare getting my contacts activity working – I understand this is a common problem, but I just can’t seem to apply existing solutions to my code – I just cannot get my head around this. Any help would be very much appreciated, I am tearing my hair out here.

public class nominateContactsActivity extends ListActivity {
public String strName;
public String strLoginCode;
public String strTelNo;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.nominatecontactsactivitytest);

    this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


    Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    startManagingCursor(cursor);
    String[] contacts = new String[] {ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};       
    String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME};
    int[] to = new int[] { android.R.id.text1 };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
            android.R.layout.simple_list_item_multiple_choice, cursor, columns, to);
    this.setListAdapter(adapter);

    Button finishButton = (Button) this.findViewById(R.id.finishButton);
    finishButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            SimpleCursorAdapter adapter = (SimpleCursorAdapter) nominateContactsActivity.this.getListAdapter();
            Cursor cursor = adapter.getCursor();

            ListView lv = nominateContactsActivity.this.getListView();
            SparseBooleanArray selectedItems = lv.getCheckedItemPositions();
            for (int i = 0; i < selectedItems.size(); i++) {

                int selectedPosition = selectedItems.keyAt(i);
                cursor.moveToPosition(selectedPosition);
                Log.d("", cursor.getString(cursor.getColumnIndex(
                        ContactsContract.Contacts.DISPLAY_NAME))+" is checked");
                Log.d("", "row id: "+adapter.getItemId(selectedPosition));
                                }
        }
    });

I am using the above code to return contacts list – this works fine, displays contact’s names in the listview as I wished. However, no phone numbers are retrieved, and I just cannot figure out how to get them.

Thank you

Answer

Below is the code to read all the contact numbers and name you can try out.

   Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
   while (phones.moveToNext())
    {
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
   }
    phones.close();

Leave a Reply

Your email address will not be published. Required fields are marked *