Dhrupal Dhrupal - 1 month ago 23
Android Question

Query db for getting list either like given name, email or phone

I want to get contacts list which is

1. LIKE given text display_name **OR**

2. LIKE given text email **OR**

3. LIKE given text phone no


Following is my query

String text = "testing";
ContentResolver cr = getContentResolver();

String[] mSelectionArgs = { "%" + text + "%" };

String SELECTION =
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?" :
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?")
+ " OR " +ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?"
+ " OR " + ContactsContract.CommonDataKinds.Email.DATA+ " LIKE ?"
;

Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
SELECTION, mSelectionArgs, null);

android.database.sqlite.SQLiteException: no such column: data1 (code 1): , while compiling: SELECT times_contacted, contacts_status_updates.status AS contact_status, phonetic_name, phonetic_name_style, link_accounts AS link, is_user_profile, lookup, phonebook_label_alt, contacts_status_updates.status_icon AS contact_status_icon, last_time_contacted, contact_last_updated_timestamp, sec_custom_vibration, _id, display_name_source, photo_uri, photo_thumb_uri, agg_presence.chat_capability AS contact_chat_capability, photo_id, send_to_voicemail, display_name_alt AS display_name_reverse, custom_ringtone, name_raw_contact_id, photo_file_id, has_phone_number, contacts_status_updates.status_label AS contact_status_label, link_type1, phonebook_bucket, display_name, phonebook_bucket_alt, has_email, sort_key_alt, phonebook_label, dirty_contact, in_visible_group, starred, link_count, display_name_alt, sort_key, agg_presence.mode AS contact_presence, sec_custom_alert, is_private, contacts_status_updates.status_res_package AS contact_status_res_package, contacts_status_updates.status_ts AS contact_status_ts FROM view_contacts_restricted LEFT OUTER JOIN agg_presence ON (view_contacts_restricted._id = agg_presence.presence_contact_id) LEFT OUTER JOIN status_updates contacts_status_updates ON (status_update_id=contacts_status_updates.status_update_data_id) WHERE ((1)) AND (((display_name LIKE ?) OR (data1 LIKE ?) OR (data1 LIKE ? )))
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:385)
at android.content.ContentResolver.query(ContentResolver.java:417)
at android.content.ContentResolver.query(ContentResolver.java:360)
at com.icpl.dhrupalpatel.searchconatact.MainActivity.getDetail(MainActivity.java:81)
at com.icpl.dhrupalpatel.searchconatact.MainActivity$1.onClick(MainActivity.java:49)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18796)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)

Answer

You can do it by using ContactsContract.Contacts.CONTENT_FILTER_URI. This is link.