我正在尝试过滤SimpleCursorAdapter ListView但是我在过滤中遇到了sql异常。 我试图找到那些名字以用户输入的文字开头的联系人。
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";
cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, null);
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return getDirectoryList(constraint);
}
});
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Toast.makeText(getApplicationContext(), "changing now"+cs
, Toast.LENGTH_LONG).show();
MyActivity.this.adapter.getFilter().filter(cs);
}
});
public Cursor getDirectoryList (CharSequence constraint) {
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " display_name like %"+constraint.toString()+"%";
return getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, null);
}
这是例外
An exception occured during performFiltering()!
android.database.sqlite.SQLiteException: near "display_name": syntax error (code 1): , while compiling: SELECT sort_key, photo_uri, send_to_voicemail, contacts_status_updates.status AS contact_status, contacts_status_updates.status_label AS contact_status_label, pinned, display_name, phonebook_label_alt, phonebook_bucket, contacts_status_updates.status_res_package AS contact_status_res_package, in_default_directory, photo_id, custom_ringtone, _id, times_contacted, phonebook_label, display_name_alt, lookup, phonetic_name, last_time_contacted, contact_last_updated_timestamp, has_phone_number, in_visible_group, display_name_source, photo_file_id, is_user_profile, contacts_status_updates.status_ts AS contact_status_ts, sort_key_alt, phonebook_bucket_alt, agg_presence.mode AS contact_presence, starred, photo_thumb_uri, contacts_status_updates.status_icon AS contact_status_icon, agg_presence.chat_capability AS contact_chat_capability, phonetic_name_style, name_raw_contact_id FROM view_contacts LEFT OUTER JOIN agg_presence ON (_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 ((has_phone_number display_name like %ab%))
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:478)
at android.content.ContentResolver.query(ContentResolver.java:422)
at com.websmithing.gpstracker.GpsTrackerActivity.getDirectoryList(GpsTrackerActivity.java:189)
at com.websmithing.gpstracker.GpsTrackerActivity$2.runQuery(GpsTrackerActivity.java:128)
at android.widget.CursorAdapter.runQueryOnBackgroundThread(CursorAdapter.java:395)
at android.widget.CursorFilter.performFiltering(CursorFilter.java:49)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
答案 0 :(得分:1)
而不是使用
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " display_name like %"+constraint.toString()+"%";
使用
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " and display_name like '%"+constraint.toString()+"%'";
这可以解决问题