Contacts for Android: why you need to request every time to use Contacts Phone List

Each many examples (website, tutorial, ebook ...), when we need to see the phone number of one contact ID, we need to query as below:

Cursor contactCursor = getContentResolver().query(uri, null, null, null, null);         
if (contactCursor.moveToFirst()){
            id = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts._ID));

        Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                                        new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
                                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id + " AND " + ContactsContract.CommonDataKinds.Phone.TYPE+ "=" +ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE ,
                                                        null, null);


But, if I ask directly in one contact uri

and filter column _ID = contactID. I will encounter an error when requesting. Here's my code:

String[] projector = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
        String selection = ContactsContract.CommonDataKinds.Phone.TYPE + "=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
        Cursor phoneContact = getContentResolver().query(uri, projector, selection, null, null);


Notes: The uri object in the above code is returned when someone selects one contact from the contact list.

@Edit: here is my error I am getting: Here's my error:

java.lang.RuntimeException: ResultInfo rejection {who = null, request = 0, result = -1, data = Intent {DAT = content: // flg = 0x1}} to action {com.basic /}: java.lang.IllegalArgumentException: Invalid column data1



source to share

All Articles