Get details of contact in single query
android, android-contacts, android-contentprovider
Solution
Maybe problem will be at `selection`. Replace yours method with mine.
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
ContactsContract.CommonDataKinds.Photo.PHOTO};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = ?";
String[] selectionArgs = { String.valueOf(1) };
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor contacts = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
You should always use `parametrized statements`. Your approach is dangerous. And your URI was bad.
Problem
Hi all I am trying to get contact information from the contact database using ContentResolver with these field want to get name, number, FORMATTED_ADDRESS, PHOTO details for a contact in one single query. So basically I need to make 3 queries per contact to obtain these details. What I want to know is that, is there a simpler and more efficient way to achieve what this. but using the below code i am getting exception. ``` java.lang.IllegalArgumentException: Invalid column data1 ``` Can any body help me for finding the solution for the same. ``` Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, ContactsContract.CommonDataKinds.Photo.PHOTO}; String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'"; String[] selectionArgs = null; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; Cursor contacts = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); ```