Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 筛选除帐户类型组之外的联系人_Android_Android Listview_Android Contentprovider - Fatal编程技术网

Android 筛选除帐户类型组之外的联系人

Android 筛选除帐户类型组之外的联系人,android,android-listview,android-contentprovider,Android,Android Listview,Android Contentprovider,我需要建立一个联系人选择器,列出所有电话的联系人,除了帐户类型的联系人。 我确实是这样做的: 示例我要从列表中删除的帐户类型是:group=“com.account.myaccount” 首先,我查询所有具有帐户类型的组id: public String[] getGroupIds(Context context, String group){ final Cursor groupCursor = context.getContentResolver().query(

我需要建立一个联系人选择器,列出所有电话的联系人,除了帐户类型的联系人。 我确实是这样做的: 示例我要从列表中删除的帐户类型是:group=“com.account.myaccount”

首先,我查询所有具有帐户类型的组id:

    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 + "=?";
}
然后我查询除组之外的所有联系人:

    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};

}
公共游标getContactGroupId(字符串[]GroupId,上下文){
字符串选择=ContactsQuery.COLUMN_NAME+“NOTNULL)和(”
+ContactsQuery.COLUMN_NAME+“!=”)和“;
StringBuffer=新的StringBuffer(选择);
for(int i=0;i0){
delete(buffer.length()-6,buffer.length());
}
selection=buffer.toString();
Log.v(标签,“选择:”+选择);
字符串排序器=Contacts contract.Contacts.DISPLAY_NAME+“整理本地化ASC”;
Cursor Cursor=context.getContentResolver().query(ContactsQuery.CONTENT\u URI,
ContactsQuery.PROJECTION、selection、groupid、sortOrder);
返回光标;
}
最终公共静态类ContactsQuery{
私人联络处(){
}
public static final String COLUMN\u NAME=Data.DISPLAY\u NAME;
公共静态最终字符串列\u ID=Data.\u ID;
public static final String COLUMN\u CONTACT\u ID=Data.CONTACT\u ID;
公共静态最终Uri内容\u Uri=Data.CONTENT\u Uri;
公共静态最终字符串[]投影=
新字符串[]{COLUMN_NAME,COLUMN_CONTACT_ID};
}
并结合所有:

    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;
}
公共列表getContactExceptGroup(上下文,字符串组){
字符串[]groupIds=getgroupidsException(上下文,组);
列表结果=新建ArrayList();
Set seen=新的HashSet();
Cursor Cursor=this.getContactGroupId(GroupId,上下文);
int indexOfContactId=cursor.getColumnIndex(ContactsQuery.COLUMN\u CONTACT\u ID);
int indexOfName=cursor.getColumnIndex(ContactsQuery.COLUMN\u NAME);
while(cursor.moveToNext()){
long contactID=cursor.getLong(indexOfContactId);
如果(!seen.contains(contactID)){//删除重复的联系人
seen.add(contactID);
PickerContactRow contactRow=新的PickerContactRow();
contactRow.setId(contactID);
contactRow.setName(cursor.getString(indexOfName));
Log.v(标签,“行:”+contactRow);
//+组:“+cursor.getString(columnGroup));
结果。添加(contactRow);
}
}
返回结果;
}
问题是显示了一些奇怪的帐户名。一些电子邮件,而不是帐户名。 请参见我的图片:

请帮我解决这个问题。或者你有没有其他方法来过滤账户。 我正在寻找可以使用游标适配器的解决方案。我不能在我的方法中使用游标适配器。

我自己决定。 我确实是这样想的:

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};

}
public Cursor getContactGroupId(字符串acountType,上下文){
字符串sortOrder=ContactsQuery.COLUMN\u NAME+“整理本地化ASC”;
Cursor Cursor=context.getContentResolver().query(ContactsQuery.CONTENT\u URI,
ContactsQuery.PROJECTION、ContactsQuery.SELECTION、null、sortOrder);
返回光标;
}
私有布尔值isAccountType(上下文上下文、字符串accountType、长id){
Cursor Cursor=context.getContentResolver().query(RawContactsQuery.CONTENT\u URI,
RawContactsQuery.PROJECTION,RawContactsQuery.SELECTION,新字符串[]{id+“”,accountType},null);
布尔结果=cursor.getCount()>0;
cursor.close();
返回结果;
}
公共列表getContactExceptGroup(上下文上下文,字符串accountType){
列表结果=新建ArrayList();
Cursor Cursor=this.getContactGroupId(accountType,context);
EmailValidator validator=新的EmailValidator();
int indexOfContactId=cursor.getColumnIndex(ContactsQuery.COLUMN\u CONTACT\u ID);
int indexOfName=cursor.getColumnIndex(ContactsQuery.COLUMN\u NAME);
final int columnGroup=cursor.getColumnIndex(ContactsQuery.DELETED);
while(cursor.moveToNext()){
long id=cursor.getLong(indexOfContactId);
如果(!isAccountType(上下文、accountType、id))
{
//seen.add(contactID);
PickerContactRow contactRow=新的PickerContactRow();
contactRow.setId(id);
contactRow.setName(cursor.getString(indexOfName));
Log.v(标签,“行:”+联系人行
+“值:”+cursor.getString(columnGroup));
结果。添加(contactRow);
}
}
cursor.close();
返回结果;
}
最终公共静态类ContactsQuery{
私人联络处(){
}
public static final String COLUMN\u NAME=Contacts.DISPLAY\u NAME;
公共静态最终字符串列\u ID=联系人。\u ID;
public static final String DELETED=Contacts.HAS_PHONE_NUMBER;
公共静态最终字符串列\u CONTACT\u ID=Contacts.\u ID;
public static final Uri CONTENT\u Uri=Contacts.CONTENT\u Uri;
公共静态最终字符串选择=列名称+“NOTNULL”
+)和(“+列名称+”!=“””
+)和(“+Contacts.HAS_PHONE_NUMBER+”=1”;
公共静态最终字符串[]投影=
新字符串[]{COLUMN_NAME,COLUMN_CONTACT_ID,DELETED};
}
最终公共静态类RawContactsQuery{
私人住宅{
}
公共静态最终字符串ACCOUNT\u TYPE=RawContacts.ACCOUNT\u TYPE;
public static final String COLUMN\u CONTACT\u ID=RawContacts.CONTACT\u ID;
public static final Uri CONTENT\u Uri=RawContacts.CONTENT\u Uri;
公共静态最终字符串选择=列\联系人\ ID+“=?)和(“+帐户\类型+”=?”;
公共街