gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:03:37 26 4
gpt4 key购买 nike

我正在尝试获取使用联系人的电子邮件 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com