【问题标题】:Android : I am trying to search for contacts on basis of their display nameAndroid:我正在尝试根据他们的显示名称搜索联系人
【发布时间】:2017-04-20 23:27:15
【问题描述】:

我正在尝试根据显示名称搜索联系人。 我从 android 开发者网站上给出的示例代码中获取了参考。

https://developer.android.com/training/contacts-provider/retrieve-names.html

在此示例中,搜索从联系人中存在的整个联系人详细信息执行,而不仅仅是联系人姓名。

例如,如果用户在他的联系人中有一个电子邮件地址,那么搜索也在匹配电子邮件中执行。 或者如果一个数字存储在 home type 中,那么在输入 h 时,home category 中的数字也会显示出来。

我希望搜索仅限于他们的显示名称。

     final static String SELECTION =
            (Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
                    "<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";

这些是我从示例代码中使用的 selecton 子句。

【问题讨论】:

    标签: android android-studio android-cursoradapter contactscontract


    【解决方案1】:

    试试这个

    Cursor cursor = getContentResolver().query(
                android.provider.ContactsContract.Contacts.CONTENT_URI,
                new String[] { ContactsContract.Contacts.PHOTO_ID,
                        ContactsContract.Contacts.DISPLAY_NAME,
                        ContactsContract.Contacts._ID },
                ContactsContract.Contacts.HAS_PHONE_NUMBER, null,
                ContactsContract.Contacts.DISPLAY_NAME);
    

    这个cursor 提供了所有具有任何电话号码的联系人,然后我将唯一的ID 保存在ArrayList 中,就像这样

    cursor.moveToFirst();
    
        while (cursor.moveToNext()) {
            contactsID.add(cursor.getString(2));
        }
    

    然后在选择联系人时,我使用此查找联系人号码

    Cursor cursor = getContentResolver()
                        .query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                new String[] {
                                        ContactsContract.CommonDataKinds.Phone.NUMBER,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                        + " = ?",
                                new String[] { contactsID.get(position) }, null);
                contactNumbers = new ArrayList<String>();
                while (cursor.moveToNext()) {
                    contactNumbers.add(cursor.getString(0));
                    Log.d("number", cursor.getString(0));
                }
    

    【讨论】:

    • 在哪里使用此代码?您检查过示例吗?
    【解决方案2】:

    Retrieving a List of Contacts 教程中的过滤代码在这里:

    Uri contentUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mSearchString));
    

    Contacts.CONTENT_FILTER_URI 文档中说:

    过滤字符串将用于匹配联系人的各个部分 名字

    所以这对你不利,将其替换为:

    @Override
    public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
        Uri contentUri = Contacts.CONTENT_URI; // no longer filters
        String selection = Contacts.DISPLAY_NAME_PRIMARY + " LIKE %" + mSearchString + "%";
    
        return new CursorLoader(
                getActivity(),
                contentUri,
                PROJECTION,
                selection,
                null,
                null
        );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2015-11-11
      • 1970-01-01
      • 2021-08-25
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多