Reputation: 41
Following the tutorial get android contact phone number list
I pulled the phone numbers and names of contacts, how do I get a listview clean, no duplicate contacts and possibly sorted by name?
Upvotes: 3
Views: 427
Reputation: 527
Try this;
Upvotes: 0
Reputation: 2005
Try this:
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
List<String> phoneNumbers = new ArrayList<String>();
while (phones.moveToNext()) {
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(".................."+phoneNumber);
if(!phoneNumbers.contains(phoneNumber)) {
phoneNumbers.add(phoneNumber);
}
}
Collections.sort(phoneNumbers);
In short: check if the phone number is not already in the list before adding it. It might be handy to remove any whitespace from each phone number before making the check so that you're sure no duplicates make it through.
Some more info about sorting: http://developer.android.com/reference/java/util/Collections.html#sort%28java.util.List%3CT%3E%29
Upvotes: 1