0

I have a program which reads the contacts and then displays it into the ListView. But the problem is that I have total 25k + contacts in my phone and 60% contacts are of google. So it takes too much time to read contacts. How do I read contacts having phone number ?

I'm using this code :

 Cursor phones = as.getApplicationContext().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();
Brendan Green
  • 11,676
  • 5
  • 44
  • 76
Emma
  • 21
  • 3

2 Answers2

0

Where clause can be used to filter the contacts with the phone numbers. Use the below code to retrieve the contacts.

   CursorLoader cursorLoader = new CursorLoader(getActivity(), ContactsContract.Contacts.CONTENT_URI, new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME},
            ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 ", null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE ASC;");

For reading the contacts you need the following permission.

<uses-permission android:name="android.permission.READ_CONTACTS" />
Kartheek
  • 7,104
  • 3
  • 30
  • 44
  • Thanks! But I'm using this : How do I edit this code ? : `ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);` – Emma Jun 23 '15 at 06:08
0

You can use HAS_PHONE_NUMBER. ContactsContract.Contacts.HAS_PHONE_NUMBER . An Example

  String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

    if (hasPhone.equals(ContactsContract.Contacts.HAS_PHONE_NUMBER))
      //do your thing
Emil
  • 2,786
  • 20
  • 24