除了帐户类型的组过滤器触点 [英] Filter contacts except groups of account type

查看:117
本文介绍了除了帐户类型的组过滤器触点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个联系人选取其中除了列出的账户类型接触的所有电话的联系人。
我没有按照这个方式:
例如帐户类型我想从列表中删除是:A组=com.account.myaccount

I need to build a contact picker which list all phone's contact except an account type of contact. I did follow this way : Example the account type I want to remove from the list is : group = "com.account.myaccount"

首先,我查询所有组ID有帐户类型:

First I query all the group id have the account type :

    public String[] getGroupIds(Context context, String group){

    final Cursor groupCursor = context.getContentResolver().query(
            GroupWrap.CONTENT_URI,
            GroupWrap.PROJECTION,
            GroupWrap.SELECTION_GROUP_ID,
            new String[]{group},
            null);

    String[] ids = new String[groupCursor.getCount()];
    int i = 0;
    final int contactNumberColumnType   = groupCursor.getColumnIndex(GroupWrap.COLUMN_TYPE); 
    final int contactNumberColumnIndex  = groupCursor.getColumnIndex(GroupWrap.COLUMN_ID);


    while (groupCursor.moveToNext()) {
        ids[i++] = Integer.toString(groupCursor.getInt(contactNumberColumnIndex));

        Log.v(TAG, " account type : " + groupCursor.getString(contactNumberColumnType)
                 + " : ids[]" + groupCursor.getInt(contactNumberColumnIndex)); 

    }



    return ids;
}

final private static class GroupWrap {

    private GroupWrap() {
    }

    public static final String[] PROJECTION =
            new String[] {Groups.ACCOUNT_TYPE, Groups._ID};

    public static final String COLUMN_TYPE = Groups.ACCOUNT_TYPE;
    public static final String COLUMN_ID = Groups._ID;
    public static final Uri CONTENT_URI = Groups.CONTENT_URI;
    public static final String SELECTION_OTHER_GROUP_ID = GroupWrap.COLUMN_TYPE + "!=?";
    public static final String SELECTION_GROUP_ID = GroupWrap.COLUMN_TYPE + "=?";
}

然后我除了查询组中的所有联系人:

And then I query all the contacts except the groups :

    public  Cursor getContactGroupIDs(String[] groupIds, Context context){

    String selection = ContactsQuery.COLUMN_NAME + " NOTNULL) AND ("

            + ContactsQuery.COLUMN_NAME + " !='' ) AND ";

    StringBuffer buffer = new StringBuffer(selection); 

    for(int i = 0; i < groupIds.length; i++){
        buffer.append(" (" + GroupMembership.GROUP_ROW_ID + " !=?) AND ");
    }


    // remove " ) AND "
    if(buffer.length() > 0){
        buffer.delete(buffer.length() - 6, buffer.length());
    }


    selection = buffer.toString();

    Log.v(TAG, "selection : " + selection);

    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    Cursor cursor = context.getContentResolver().query(ContactsQuery.CONTENT_URI, 
            ContactsQuery.PROJECTION, selection, groupIds, sortOrder);

    return cursor;
}


final public static class ContactsQuery {

    private ContactsQuery() {

    }

    public static final String COLUMN_NAME = Data.DISPLAY_NAME;
    public static final String COLUMN_ID = Data._ID;
    public static final String COLUMN_CONTACT_ID = Data.CONTACT_ID;
    public static final Uri CONTENT_URI = Data.CONTENT_URI;

    public static final String[] PROJECTION =
            new String[] {COLUMN_NAME, COLUMN_CONTACT_ID};

}

和combinate所有:

And combinate all :

    public List<PickerContactRow> getContactExceptGroup(Context context, String group){
    String[] groupIds = getGroupIdsExcept(context, group);
    List<PickerContactRow> result = new ArrayList<PickerContactRow>();

    Set<Long> seen = new HashSet<Long>();

    Cursor cursor = this.getContactGroupIDs(groupIds, context);


    int indexOfContactId = cursor.getColumnIndex(ContactsQuery.COLUMN_CONTACT_ID);
    int indexOfName = cursor.getColumnIndex(ContactsQuery.COLUMN_NAME);


    while (cursor.moveToNext()) {
        long contactID = cursor.getLong(indexOfContactId);
        if (!seen.contains(contactID)) {// remove duplicate contact
            seen.add(contactID);
            PickerContactRow contactRow = new PickerContactRow();
            contactRow.setId(contactID);
            contactRow.setName(cursor.getString(indexOfName));

            Log.v(TAG, "row : " + contactRow); 
                    //+ "group : " + cursor.getString(columnGroup));

            result.add(contactRow);
        }
    }

    return result;
}

问题是有显示一些奇怪的帐户名。有些电子邮件而不是帐户名。
看到我的图像:

The problem is there are some strange account name is displayed. Some email instead of account name. See on my image :

请帮我解决这个问题。如果你还有任何其他方式过滤的帐户。
我找的,我可以用一个CursorAdapter的解决方案。我不能使用的CursorAdapter用我的方法。

Please help me resolve the problem. Or do you have any other way to filter the accounts. I am looking for the solution that I can use a CursorAdapter. I can't use a CursorAdapter with my approach.

推荐答案

我自己解决。
我没有流是这样的:

I resolved by myself. I did flow this way :

public  Cursor getContactGroupIDs(String acountType, Context context){

    String sortOrder = ContactsQuery.COLUMN_NAME + " COLLATE LOCALIZED ASC";

    Cursor cursor = context.getContentResolver().query(ContactsQuery.CONTENT_URI, 
            ContactsQuery.PROJECTION, ContactsQuery.SELECTION , null, sortOrder);

    return cursor;
}

private boolean isAccountType(Context context, String accountType, long id){
    Cursor cursor = context.getContentResolver().query(RawContactsQuery.CONTENT_URI, 
            RawContactsQuery.PROJECTION, RawContactsQuery.SELECTION , new String[]{id + "", accountType}, null);
    boolean result = cursor.getCount() > 0;
    cursor.close();
    return result;
}

public List<PickerContactRow> getContactExceptGroup(Context context, String accountType){
    List<PickerContactRow> result = new ArrayList<PickerContactRow>();

    Cursor cursor = this.getContactGroupIDs(accountType, context);
    EmailValidator validator = new EmailValidator();

    int indexOfContactId = cursor.getColumnIndex(ContactsQuery.COLUMN_CONTACT_ID);
    int indexOfName = cursor.getColumnIndex(ContactsQuery.COLUMN_NAME);
    final int columnGroup   = cursor.getColumnIndex(ContactsQuery.DELETED);

    while (cursor.moveToNext()) {
        long id = cursor.getLong(indexOfContactId);
        if (!isAccountType(context, accountType, id)) 
        {
            //seen.add(contactID);
            PickerContactRow contactRow = new PickerContactRow();
            contactRow.setId(id);
            contactRow.setName(cursor.getString(indexOfName));

            Log.v(TAG, "row : " + contactRow
                    + " value : " + cursor.getString(columnGroup));



            result.add(contactRow);
        }
    }
    cursor.close();
    return result;
}


final public static class ContactsQuery {

    private ContactsQuery() {

    }

    public static final String COLUMN_NAME = Contacts.DISPLAY_NAME;
    public static final String COLUMN_ID = Contacts._ID;
    public static final String DELETED = Contacts.HAS_PHONE_NUMBER;
    public static final String COLUMN_CONTACT_ID =  Contacts._ID;
    public static final Uri CONTENT_URI = Contacts.CONTENT_URI;
    public static final String SELECTION = COLUMN_NAME + " NOTNULL"
            + ") AND (" + COLUMN_NAME + " !=''"
             + ") AND (" + Contacts.HAS_PHONE_NUMBER + " =1";


    public static final String[] PROJECTION =
            new String[] {COLUMN_NAME, COLUMN_CONTACT_ID, DELETED};

}


final public static class RawContactsQuery {

    private RawContactsQuery() {

    }

    public static final String ACCOUNT_TYPE = RawContacts.ACCOUNT_TYPE;
    public static final String COLUMN_CONTACT_ID = RawContacts.CONTACT_ID;
    public static final Uri CONTENT_URI = RawContacts.CONTENT_URI;
    public static final String SELECTION = COLUMN_CONTACT_ID + "=?) AND (" + ACCOUNT_TYPE + " =?";

    public static final String[] PROJECTION =
            new String[] {COLUMN_CONTACT_ID};

}

我看到它很慢,但我无法找到其他的办法。

I see it quite slow, but I couldn't find out other approach.

这篇关于除了帐户类型的组过滤器触点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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