Using CursorLoader to get emails causes duplication of emails

前端 未结 6 2197
攒了一身酷
攒了一身酷 2021-02-14 06:44

I am trying to get email ids of uses contacts. For that I am using Cursor Loader. There is one problem I am getting duplicate email ids also. How to remove email duplicacy. Shou

6条回答
  •  囚心锁ツ
    2021-02-14 07:35

    I used a small hack in my project - an SQL injection, like that:

    @Override
    public Loader onCreateLoader(int id, Bundle args) {
        return new CursorLoader(
                this,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] {
                    "DISTINCT "+ MediaStore.Images.Media.BUCKET_ID,
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME},
                null, null, null);
    }
    

    This code returns only bundle names and their IDs from Gallery. So, I'd rewrite your code like that:

    @Override
    public Loader onCreateLoader(int arg0, Bundle arg1) {
        String[] projection = new String[] {
            "DISTINCT " + ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Email.DATA};
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + "  COLLATE LOCALIZED ASC";
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +"='1' AND " + Email.DATA +" IS NOT NULL AND " + Email.DATA +" != \"\" " ;
    
        //showing only visible contacts  
        String[] selectionArgs = null;
        return new CursorLoader(this, ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
    }
    

提交回复
热议问题