android - 使用 CursorLoader 获取邮件导致邮件重复

标签 android android-contacts android-cursorloader

我正在尝试获取使用联系人的电子邮件 ID。为此,我正在使用 Cursor Loader。还有一个问题是我也收到了重复的电子邮件 ID。如何删除电子邮件重复。我应该使用原始查询“SELECT DISTINCT”而不是使用 CursorLoader 还是有其他解决方案?

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

最佳答案

我最近遇到了这个问题。看起来 CursorLoader 没有实现“DISTINCT”。我的解决方法是在 onLoadFinish 方法中添加几行代码并扩展 BaseAdapter 以接受一个 List 参数:

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

这是我的 BaseAdapter 类:

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

您应该能够根据您的需要专门修改它。

关于android - 使用 CursorLoader 获取邮件导致邮件重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14680865/

相关文章:

java - 更改内部适配器内容后在 ListView 上调用刷新

android - 获取我的个人数据联系人

android - SyncAdapter 可以修改另一个 SyncAdapter 添加的 RawContacts 吗?

android - 游标与 CursorLoader

android - 在 ListView 中显示之前合并游标中的数据

android - 在 Android View 上下文类中设置实例变量

android - 使用日历 API 摆脱 Android 日历事件中的 "Attending?"

java - 对 CONTENT_FILTER_URI 结果进行排序

java - 适用于没有 API 的 Web 应用程序的 Android 应用程序

android - 排序的联系人列表有重复项,为什么?