Reputation: 9926
i wrote a code that return all the contacts that are on my android phone. but when i run it on my android phone i see that even if i have 600 contacts - i get on my code only 173 contacts
How to get all the contacts ? Why i get only 173 of them ?
The code:
private void CollectAllContacts(Activity activity){
ArrayList<ContactData> contactDataList = new ArrayList<ContactData>();
try {
ContentResolver cr = activity.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
ContactData newContact = new ContactData();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
newContact.setId(id);
newContact.setDisplayName(name);
contactDataList.add(newContact);
}
}
}
catch(Exception e)
{
// write to log.
}
}
Upvotes: 1
Views: 50
Reputation: 2860
I was facing same problem. I resolved it by using intent. You can try this
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, 100);
then onActivityResult You can handle it
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == getActivity().RESULT_OK && null != data) {
Uri uri = data.getData();
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
String phoneNumber = people.getString(indexNumber);
phoneNumber = phoneNumber.replaceAll("[-]", "").replaceAll(" ", "");
} while (people.moveToNext());
}
}
Upvotes: 1