Get contact information in one request

Hello everyone. I am trying to get contact information from the contacts database using ContentResolver with this field to get the name, number, FORMATTED_ADDRESS, PHOTO details for the contact in one request.

So basically I need to make 3 contact requests in order to get these Details.

What I want to know is whether there is a simpler and more effective way to achieve this.

but using the code below, I get an exception.

java.lang.IllegalArgumentException: Invalid column data1 

Can any body help me find a 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); 
+4
source share
2 answers

Perhaps the problem will be in selection . Replace your 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.

+4
source

Here you go

Replace

 ContactsContract.Contacts.CONTENT_URI; 

from

 ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
+1
source

All Articles