Using CursorLoader to get emails causes duplication of emails

前端 未结 6 2196
攒了一身酷
攒了一身酷 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 recently ran into this problem. It appears that the CursorLoader does not have an implementation of "DISTINCT". My workaround adds a few lines to the onLoadFinish method and extends the BaseAdapter to accept a List parameter:

    @Override
    public Loader onCreateLoader(int id, Bundle args) {
        String projection[] = {
                CommonDataKinds.Phone._ID,
                CommonDataKinds.Phone.DISPLAY_NAME,
        };      
        String select = "((" + CommonDataKinds.Phone.DISPLAY_NAME + " NOTNULL) and " + CommonDataKinds.Phone.HAS_PHONE_NUMBER + " > 0)";
        String sort = CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
    
        CursorLoader loader = new CursorLoader(
                mContext, 
                CommonDataKinds.Phone.CONTENT_URI,
                projection,
                select,
                null,
                sort
                );  
    
        return loader;
    }
    
    @Override
    public void onLoadFinished(Loader loader, Cursor cursor) {
        List displayNames = new ArrayList();
        cursor.moveToFirst();
    
        while(!cursor.isAfterLast()){
            String name = cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
    
            if(!displayNames.contains(name))
                displayNames.add(name);
    
            cursor.moveToNext();
        }
    
        mAdapter.swapCursor(displayNames);
    }
    

    Here is my BaseAdapter class:

    public class AdapterAddContacts extends BaseAdapter{
    private List mData = new ArrayList();
    private Context mContext;
    
    public AdapterAddContacts(Context context,List displayNames){
        mData = displayNames;
        mContext = context;
    }   
    
    @Override
    public int getCount() {
        if(mData != null)
            return mData.size();
        else
            return 0;
    }
    
    @Override
    public Object getItem(int pos) {
        return mData.get(pos);
    }
    
    @Override
    public long getItemId(int id) {
        return id;
    }
    
    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.entry_add_contacts,parent,false);
    
        String data = mData.get(pos);                           
    
        TextView textName = (TextView)view.findViewById(R.id.my_contacts_add_display_name);
        textName.setText(data);
        textName.setTag(data);          
    
        return view;
    }   
    
    public void swapCursor(List displayNames){
        mData = displayNames;
        this.notifyDataSetChanged();
    }
    

    You should be able to modify this specifically for your needs.

提交回复
热议问题