Contacts: eliminate the selection of duplicate phone numbers
I am trying to select all the the contacts on the phone with a phone number. However, if someone has the same phone number phone the same contact twice, it will appear twice, since it has a different Phone._ID. Is there a way to eliminate the selection of duplicate numbers in the query:
Uri uri = Data.CONTENT_URI;
String[] projection = new String[] {
Data._ID,
Phone._ID,
Phone.LOOKUP_KEY,
Data.CONTACT_ID,
Data.RAW_CONTACT_ID,
Phone.NUMBER,
Data.DISPLAY_NAME,
Phone.LABEL,
Phone.TYPE,
Phone.PHOTO_ID
};
String selection = Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE+"'";
String[] selectionArgs = null;
String sortOrder = Data.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
Or, alternatively, in the post selec开发者_StackOverflow中文版tion process? Am not too picky...
In pure SQL you can specify UNIQUE on an ordered select. I don't know if content providers can or not. If not, use an order by with ascending phone number and name. Compare the moveToNext name to the last 'new' name. If you are using a while loop, do continue when they match.
String goodName = "";
String goodNumber = "";
while (cursor.moveToNext()) {
... get name and number from cursor ....
if (goodNumber == contactNumber) {
if (contactName == goodName) {
continue;
}
}
goodNumber = contactNumber;
goodName = contactName;
... do more work ....
精彩评论