如何在從通訊錄中獲取聯繫人號碼時刪除Android中的重複號碼條目?如何從沒有數據庫的所有類型的聯繫人中刪除重複的聯繫人號碼?
例如: 在接觸書 一個名稱的多個聯繫人.. EX型家用9428060123,
型工作9428060123,我想從所有 型機器人抓取唯一聯繫號碼有問題?我用下面的代碼獲取
信息:
private void getContactsDetails() {
showLoader();
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null/*selection + " AND " +
ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1"*/, null, "UPPER("
+ ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
ContactString = new ArrayList<>();
if (phones != null) {
if (phones.getCount() > 0) {
tvNoContact.setVisibility(View.GONE);
while (phones.moveToNext()) {
String Name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
HashSet<String> mobileNoSet = new HashSet<String>();
if (!mobileNoSet.contains(number)){
String s = number.replaceAll("\\W", "");
String lastTenCharContact = null;
if (s != null && s.length() > 10) {
lastTenCharContact = s.substring(s.length() - 10);
} else {
lastTenCharContact = s;
}
// String substring = s.substring(Math.max(s.length() - 10, 0));
Log.d(TAG, "getContactsDetails: " + lastTenCharContact);
String image_uri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
ContactString.add(lastTenCharContact);
DeviceContact contactModel = new DeviceContact(Name.toUpperCase(), lastTenCharContact, image_uri);
contactModelsList.add(contactModel);
mobileNoSet.add(number);
Log.d(TAG, "Name : " + Name + ", Number : " + number + ", Photo : " + image_uri);}
}
hideLoader();
AllContactsyncapiCall();
} else {
tvNoContact.setVisibility(View.VISIBLE);
}
} }
我在我的應用程序中使用它,它工作完美。 mobileNoSet hashSet存儲號碼,如果從光標設置contais編號,則不允許將其添加到DeviceContact contactModel中。發佈您的新代碼 –
因爲您在每個循環中都創建了新的Hashset,所以您將獲得重複條目。現在移動這一行HashSet mobileNoSet = new HashSet ();在tvNoContact.setVisibility(View.GONE)之後; –
thanx ..它的工作,但是當我的設備與whatsapp或任何其他帳戶同步比我得到同樣的問題。 –