Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CursorLoader to get emails causes duplication of emails

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. Should I use raw query "SELECT DISTINCT" instead of using CursorLoader or there is some other solution?

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    String[] projection = new String[] { 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);
}
like image 962
Gaurav Vashisth Avatar asked Feb 04 '13 05:02

Gaurav Vashisth


1 Answers

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<Cursor> 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<Cursor> loader, Cursor cursor) {
    List<String> displayNames = new ArrayList<String>();
    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<String> mData = new ArrayList<String>();
private Context mContext;

public AdapterAddContacts(Context context,List<String> 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<String> displayNames){
    mData = displayNames;
    this.notifyDataSetChanged();
}

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

like image 94
mars Avatar answered Sep 30 '22 19:09

mars