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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to retrieve contact’s phone number (Android)? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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();
We are here to answer your question about How to retrieve contact’s phone number (Android)? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji