搭载Android 4.1 + / ContactContract:查找组给定的ContactID(从查找了一些派生) [英] Android 4.1+/ContactContract: Lookup group to given contactId (derived from lookup a number)

查看:178
本文介绍了搭载Android 4.1 + / ContactContract:查找组给定的ContactID(从查找了一些派生)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的函数具有一个电话号码作为输入参数(例如,436641234567或436641234567),进行在联系人数据库中的两个查找:第一,识别属于这个数量的用户(在此已经作品),然后使用id用户的获得各组此联系人被分配给(这不工作)。测试还给正确的ID(再次),然而,该集团是空。

 公共字符串getNameGroupByNumber(上下文的背景下,串号){
    字符串名称=?;
    串的ContactID =0;
    串组=0;
    串测试=0;    //第1步:查找名来定数
    ContentResolver的ContentResolver的= context.getContentResolver();    URI URI = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.en code(数字));
    的String [] = contactProjection新的String [] {BaseColumns._ID,ContactsContract.PhoneLookup.DISPLAY_NAME};
    光标contactLookup = contentResolver.query(URI,contactProjection,NULL,NULL,NULL);    尝试{
        如果(contactLookup =空&放大器;!&放大器; contactLookup.getCount()大于0){
            contactLookup.moveToNext();            名称= contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            使用ContactID = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
        }
    } {最后
        如果(contactLookup!= NULL){
            contactLookup.close();
        }
    }    Log.d(TAG,姓名:+姓名+的ContactID:+的ContactID); //按预期工作    //步骤2:联系人的查找组成员在第一步中发现
    乌里groupURI = ContactsContract.Data.CONTENT_URI;
    的String [] = groupProjection新的String [] {ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID};
    光标groupLookup = contentResolver.query(groupURI,groupProjection,ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID +=+的ContactID,NULL,NULL);    尝试{        如果(groupLookup =空&放大器;!&放大器; groupLookup.getCount()大于0){
            groupLookup.moveToNext();             测试= groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID));
             组= groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID));             Log.d(TAG,集团发现ID:+测试+和GroupId的:+组); //再次测试是从上面的ContactID但组为空        }
    } {最后
        如果(groupLookup!= NULL){
            groupLookup.close();
        }
    }    返回名称;
 }


解决方案

 光标groupLookup = getContentResolver()查询(
        ContactsContract.Data.CONTENT_URI,
        新的String [] {
                ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,
                ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID
        },
        ContactsContract.Data.MIMETYPE +=?和+ ContactContract.Data.CONTACT_ID +=?,
        新的String [] {
                ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,
                CONTACT_ID
        }, 空值
);

使用此 groupLookup 光标来代替。

 光标groupCursor = getContentResolver()查询(
        ContactsContract.Groups.CONTENT_URI,
        新的String [] {
                ContactsContract.Groups._ID,
                ContactsContract.Groups.TITLE
        },ContactsContract.Groups._ID +=+ _id,NULL,NULL
);

groupCursor 有助于从_id获得小组冠军。

The following function has a phone number as input parameter (e.g. +436641234567 or +436641234567) and performs two lookups in the Contacts database: first, identify the user belonging to this number (this already works) and then to use the id of the user to get all groups this contact is assigned to (and this does not work). Test gives back the correct id (again), however, the group is "null".

    public String getNameGroupByNumber(Context context, String number) {
    String name = "?";
    String contactId = "0";
    String group = "0";
    String test = "0";

    // Step 1: LookUp Name to given Number
    ContentResolver contentResolver = context.getContentResolver();

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] contactProjection = new String[] {BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME };
    Cursor contactLookup = contentResolver.query(uri, contactProjection, null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();

            name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));     
        }
    } finally {         
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    Log.d(TAG, "Name: " +name + " ContactId: " + contactId);    // works as expected

    // Step 2: Lookup group memberships of the contact found in step one
    Uri groupURI = ContactsContract.Data.CONTENT_URI;
    String[] groupProjection = new String[]{ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID , ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID};
    Cursor groupLookup = contentResolver.query(groupURI, groupProjection, ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID+"="+contactId, null, null);

    try {

        if (groupLookup != null && groupLookup.getCount() > 0) {
            groupLookup.moveToNext();

             test = groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID));
             group = groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID));

             Log.d(TAG, "Group found with Id: " + test + " and GroupId: " + group);   // test is again the contactID from above but group is null

        }
    } finally {         
        if (groupLookup != null) {
            groupLookup.close();
        }
    }

    return name;
 }

解决方案

Cursor groupLookup = getContentResolver().query(
        ContactsContract.Data.CONTENT_URI,
        new String[]{
                ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID , 
                ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID
        },
        ContactsContract.Data.MIMETYPE + "=? AND " + ContactContract.Data.CONTACT_ID + "=?",
        new String[]{
                ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,
                contact_id
        }, null
);

Use this groupLookup cursor instead.

Cursor groupCursor = getContentResolver().query(
        ContactsContract.Groups.CONTENT_URI,
        new String[]{
                ContactsContract.Groups._ID,
                ContactsContract.Groups.TITLE
        }, ContactsContract.Groups._ID + "=" + _id, null, null
);

groupCursor helps to get Group title from _id.

这篇关于搭载Android 4.1 + / ContactContract:查找组给定的ContactID(从查找了一些派生)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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