5

I am making a small app where I can get the contacts of my phone using a content provider and display them in a listview as illustrated.

enter image description here

I want to select a row of the listview and automically make a phone call to that specific contact. I tried some things,but they don't work. Any ideas? Here is my code.

public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener{
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI,
            new String[] {ContactsContract.Contacts.DISPLAY_NAME},
            null, null, null);

    List<String> contacts = new ArrayList<String>();
    if (c.moveToFirst()) {
        do {
            contacts.add(c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        } while (c.moveToNext());
    }
    adapter = new ArrayAdapter<String>(this, R.layout.activity_main, contacts);
    setListAdapter(adapter);



}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      //The answer should be inside here.

  }
}
Theo
  • 3,099
  • 12
  • 53
  • 94
  • 1
    do not create a POJO class, do not iterate over the `Cursor` in a loop, do not use `ArrayAdapter`, what to do instead? use `SimpleCursorAdapter`, that's all – pskink Oct 10 '15 at 08:58
  • @pskink Good call. I always forget about the CursorAdapters, for some reason. Never used 'em much, – Mike M. Oct 10 '15 at 09:11
  • @MikeM. at least you KNOW that using them with a `Cursor` based data model is much better than using POJO/loop/ArrayAdapter, 95% guys here DONT and what is worse they are so stubborn in using bad practices... – pskink Oct 10 '15 at 09:17

2 Answers2

4

First, ensure that you have added the Permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

UPDATE: On Android 6 and higher stating the permission in the manifest is not enough, you have to explicitly ask user for granting permission on reading contacts otherwise, you will get an exception. See this answer for more details.

Then you can loop through your phone contacts like this:

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
while (cursor.moveToNext()) { 
    String contactId = cursor.getString(cursor.getColumnIndex( 
    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

    if (Boolean.parseBoolean(hasPhone)) { 
        // You know it has a number so now query it like this
        Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
        while (phones.moveToNext()) { 
            String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));                 
        } 
        phones.close(); 
    }

    Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 

    while (emails.moveToNext()) { 
        // This would allow you get several email addresses 
        String emailAddress = emails.getString( 
        emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
    } 

    emails.close();
}
Michael Abyzov
  • 452
  • 1
  • 6
  • 16
Rajan Bhavsar
  • 1,977
  • 11
  • 25
1

Try with:

private void doMagicContacts() {
    Cursor cursor = getContentResolver()
            .query(ContactsContract.Contacts.CONTENT_URI,
                    null,
                    null,
                    null,
                    null);

    if (cursor == null) {
        return;
    }

    cursor.moveToFirst();

    do {
        String name = cursor.getString(
                cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String id = cursor.getString(
                cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));

        Cursor phones = getContentResolver()
                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID+" = " + id,
                        null,
                        null);
        if (phones != null) {
            while (phones.moveToNext()) {
                String phoneNumber = phones.getString(
                        phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Log.d(TAG, "doMagicContacts: " + name + " " + phoneNumber);
            }
            phones.close();
        }

    } while (cursor.moveToNext());

    cursor.close();
}
Michael Abyzov
  • 452
  • 1
  • 6
  • 16
Evin1_
  • 12,292
  • 9
  • 45
  • 47