10

表示名で連絡先を検索しようとしています。目標は、この連絡先を開いてデータを追加することです(具体的には、より多くの電話番号)が、更新したい連絡先を見つけるのに苦労しています。

これは私が使用しているコードです:

    public static String findContact(Context context) {

    ContentResolver contentResolver = context.getContentResolver();
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI;
    String[] projection = new String[] { PhoneLookup._ID };
    String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ?";
    String[] selectionArguments = { "John Johnson" };
    Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);

    if (cursor != null) {
        while (cursor.moveToNext()) {
            return cursor.getString(0);
        }
    }
    return "John Johnson not found";
}

「JohnJohnson」という連絡先がありますが、メソッドは常に「notfound」を返します。また、名前が1つだけの連絡先を検索してみたので、違いはありません。

特定の表示名の連絡先をオンラインで検索する例がオンラインで見つからなかったため、URI、選択、または選択の引数に問題があると思われます。表示名は、たとえばとは異なる特別な種類の情報のようです。電話番号。

ジョン・ジョンソンを見つけるために私がどのように達成できるアイデアはありますか?


更新:表示名で連絡先を見つける方法を見つけました:

        ContentResolver contentResolver = context.getContentResolver();
    Uri uri = Data.CONTENT_URI;
    String[] projection = new String[] { PhoneLookup._ID };
    String selection = StructuredName.DISPLAY_NAME + " = ?";
    String[] selectionArguments = { "John Johnson" };
    Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);

    if (cursor != null) {
        while (cursor.moveToNext()) {
            return cursor.getString(0);
        }
    }
    return "John Johnson not found";

このコードは、表示名が「JohnJohnson」の最初の連絡先の連絡先IDを返します。元のコードでは、クエリでURIと選択が間違っていました。

4

4 に答える 4

1

この問題は、設定した予測が原因である可能性があると思います。投影は、クエリするデータの列をAndroidに通知するために使用されます。その後、id列のみを指定して、表示名が返されないようにします。投影を削除して、機能するかどうかを確認してください。

-- Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);
++ Cursor cursor = contentResolver.query(uri, null, selection, selectionArguments, null);

于 2012-03-09T02:37:06.623 に答える
0
           //method for gaining id
//this method get  a name  and make fetch it's id  and then send the id to other method //named "showinformation" and that method print information of that contact  
         public void id_return(String name) {
                String id_name=null;
                Uri resultUri = ContactsContract.Contacts.CONTENT_URI;
                Cursor cont = getContentResolver().query(resultUri, null, null, null, null);
                String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " = ?" ; 
                String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,name};
                Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
                while (nameCur.moveToNext()) {
                id_name = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID));}
                nameCur.close();
                cont.close();
                nameCur.close();
//for calling of following method
                showinformation(id_name);
            }

            //method for showing information like name ,phone, email and other thing you want
            public void showinformation(String  id) {
                String name=null;
                String phone=null;
                String email=null;
                Uri resultUri = ContactsContract.Contacts.CONTENT_URI;
                Cursor cont = getContentResolver().query(resultUri, null, null, null, null);
                String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID+ " = ?" ; 

                String[] whereNameParams1 = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,id};
                Cursor nameCur1 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams1, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
                while (nameCur1.moveToNext()) {
                name = nameCur1.getString(nameCur1.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));}
                nameCur1.close();
                cont.close();
                nameCur1.close();


                String[] whereNameParams2 = new String[] { ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,id};
                Cursor nameCur2 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams2, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
                while (nameCur2.moveToNext()) {
                phone = nameCur2.getString(nameCur2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));}
                nameCur2.close();
                cont.close();
                nameCur2.close();


                String[] whereNameParams3 = new String[] { ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,id};
                Cursor nameCur3 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams3, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
                while (nameCur3.moveToNext()) {
                email = nameCur3.getString(nameCur3.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));}
                nameCur3.close();
                cont.close();
                nameCur3.close();

                String[] whereNameParams4 = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,id};
                Cursor nameCur4 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams4, ContactsContract.CommonDataKinds.StructuredPostal.DATA);
                while (nameCur4.moveToNext()) {
                phone = nameCur4.getString(nameCur4.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA));}
                nameCur4.close();
                cont.close();
                nameCur4.close();
    //showing result
             txadd.setText("Name= "+ name+"\nPhone= "+phone+"\nEmail= "+email);  


            }

 //thank all persons in this site because of many help of me to learn and correction my warn and errors this is only a gift for all of you and ...
于 2014-08-17T17:20:27.817 に答える
0

以下のコードはトリックを行う必要があります

  if (displayName != null) {
        Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(displayName));
        String[] displayNameProjection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME };
        Cursor cur = context.getContentResolver().query(lookupUri, displayNameProjection, null, null, null);
        try {
            if (cur.moveToFirst()) {
                return true;
            }
        } finally {
            if (cur != null)
                cur.close();
        }
        return false;
    } else {
        return false;
    }

参照:連絡先のリストの取得記事

于 2017-07-19T03:47:51.347 に答える
0

クエリURIを変更します。

電話番号のみをフィルタリングするためのURIを使用しています。

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI;

display_name次のように、列にアクセスできるURIを使用する必要があります。

Uri uri = ContactsContract.Data.CONTENT_URI;

Android SDKドキュメントでは、使用するURIとそれらをいつ使用するかについて適切な内訳があります。

  • 個々の連絡先を読み取る必要がある場合は、CONTENT_URIの代わりにCONTENT_LOOKUP_URIを使用することを検討してください。

  • 電話番号で連絡先を検索する必要がある場合は、この目的のために最適化されたPhoneLookup.CONTENT_FILTER_URIを使用します。

  • 連絡先を部分的な名前で検索する必要がある場合、たとえば、入力時にフィルターを作成する場合は、CONTENT_FILTER_URIURIを使用します。

  • メールアドレスやニックネームなどのデータ要素で連絡先を検索する必要がある場合は、ContactsContract.Dataテーブルに対してクエリを使用します。結果には、連絡先ID、名前などが含まれます。

于 2018-05-10T03:43:49.487 に答える