【问题标题】:Adding constraints to my searchable.xml向我的 searchable.xml 添加约束
【发布时间】:2016-04-03 09:07:56
【问题描述】:

我正在使用以下 xml 文件为我的 SearchView 实现自动建议:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/find_contact"
android:hint="@string/find_contact"
android:gravity="center"
android:includeInGlobalSearch="true"
android:queryAfterZeroResults="true"
android:searchMode="queryRewriteFromText"
android:searchSettingsDescription="@string/find_contact"
android:searchSuggestAuthority="com.android.contacts"
android:searchSuggestIntentAction="android.provider.Contacts.SEARCH_SUGGESTION_CLICKED"
android:searchSuggestIntentData="content://com.android.contacts/contacts/lookup" >

<!-- allow green action key for search-bar and per-suggestion clicks -->
<actionkey
    android:keycode="KEYCODE_CALL"
    android:queryActionMsg="call"
    android:suggestActionMsg="call" />

</searchable>

问题在于,除了建议有效联系人之外,它还提供联系人和其他“虚假”联系人中电子邮件 ID 的建议。

我想添加一个约束,只为有效的电话联系人提供建议,即有电话号码的联系人。

如何实现?

【问题讨论】:

    标签: android android-contacts android-contentresolver android-search


    【解决方案1】:

    在我看来,唯一的方法是扩展 ContentProvider 以查询 ContactProvider 以获得所需的实体,并将其分配到您的 searchable.xml 中作为您的 searchSuggestAuthority。

    http://developer.android.com/guide/topics/providers/content-provider-creating.html

    ContactsContract.Contacts 类还包含 HAS_PHONE_NUMBER 列,因此您可以通过过滤该字段来查询联系人提供程序,如下所示:

    context.getContentResolver().query(ContactsContract.Contact.CONTENT_LOOKUP_URI, new String[]{ContactsContract.Contact._ID}, ContactsContract.Contact.HAS_PHONE_NUMBER + "=?", new String[]{"1"}, null), true);
    

    我之前没有检查过代码,但我希望我能给出一些好的提示;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多