我有一个联系人列表,其中包含多个具有相同电子邮件ID的名称。如何修改以下代码,以便我只获得具有不同电子邮件ID的名称。
public ArrayList<String> getNameEmailDetails(){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
return names;
}
答案 0 :(得分:1)
sqlite中有一个游标来获取不同的值,true的用法仅给出了不同的值
Cursor cursor = sqliteDB.query(true,"window",new String[]{"MediaType","_id",TAG_DATA_OR_LOCATION}, null, null,"DataOrLocation", null, null, null);
public Cursor query(boolean distinct,String table,String [] columns,String selection,String [] selectionArgs,String groupBy,String having,String orderBy,String limit)