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));
        }
        contactCursor.close();

        //CHOOSE MOBILE PHONE OF CONTACT_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: //com.android.contacts/contacts/lookup/0r2-354F574135474F2741334D352743/2 flg = 0x1}} to action {com.basic / com.data.SMSContactApp}: java.lang.IllegalArgumentException: Invalid column data1

Thank:)

+3


source to share





All Articles