查找与给定的显示名称接触 [英] Find contacts with given display name

查看:117
本文介绍了查找与给定的显示名称接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习Android的我试图找到使用DISPLAY_NAME选择联系人。我需要找到指定名称的所有联系人。一切顺利伟大的使用标准查询,但是当我使用ContentProviderOperation下降。我不明白的一些功能。调试时我看到异常:空值。但是,哪些值我必须插入呢?谢谢你。

  op.add(ContentProviderOperation.newAssertQuery(ContactsContract.Contacts.CONTENT_URI)
      .withSelection(ContactsContract.Contacts.DISPLAY_NAME +=+姓名+',新的String [] {} ContactsContract.Contacts._ID)
      。建立());
    尝试 {
        结果= getContentResolver()applyBatch(ContactsContract.AUTHORITY,OP)。
    }赶上(例外五){

    }
 

解决方案

问题是与code你的第二个行应阅读:

  .withSelection(
    ContactsContract.Contacts.DISPLAY_NAME +=?,
    新的String [] {名}
)
 

要说明,该withSelection方法有两个参数,选择字符串和数组selectionArgs两个,这些代入查询编译过程中选择字符串。因此,在这个例子中,是subtituted为名称的值。文本识别符(单引号)将被自动嵌入因此不需要额外的努力,同时

Learning Android I'm trying to find contacts using DISPLAY_NAME selector. I need to find all contacts with given name. Everything goes great using standard query, but falls when I use ContentProviderOperation. I do not understand some of features. When debugging i see exception: Empty values. But, which values I must insert there? Thanks.

    op.add(ContentProviderOperation.newAssertQuery(ContactsContract.Contacts.CONTENT_URI)
      .withSelection(ContactsContract.Contacts.DISPLAY_NAME + " = '" + name + "'", new String[] {ContactsContract.Contacts._ID})
      .build());        
    try {
        result = getContentResolver().applyBatch(ContactsContract.AUTHORITY, op);
    } catch (Exception e) {

    }       

解决方案

the problem is with your second line of code which should read:

.withSelection(
    ContactsContract.Contacts.DISPLAY_NAME + " = ?",
    new String[] {name}
)

To explain, the withSelection method takes two parameters, the selection string and an array selectionArgs, these are substituted into the selection string during query compilation. So in this example the ? is subtituted for the value of name. Text qualifiers (single quotes) are automatically embedded at the same time so no additional effort is required

这篇关于查找与给定的显示名称接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆