Android 如何在没有数据库的情况下从所有类型的联系人中删除重复的联系人号码?
从联系人簿中提取联系人号码时,如何删除Android中的重复号码条目 例如: 在联系人簿中 一名多联系人。。ex-type-home-9428060123,Android 如何在没有数据库的情况下从所有类型的联系人中删除重复的联系人号码?,android,contacts,android-contacts,Android,Contacts,Android Contacts,从联系人簿中提取联系人号码时,如何删除Android中的重复号码条目 例如: 在联系人簿中 一名多联系人。。ex-type-home-9428060123, 键入-work-9428060123,我想从所有联系人中获取唯一的联系人号码 类型,android有问题吗?我使用下面的代码获取 资料: private void getContactsDetails() { showLoader(); String selection = ContactsContract.Cont
键入-work-9428060123,我想从所有联系人中获取唯一的联系人号码 类型,android有问题吗?我使用下面的代码获取
资料:
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);
}
} }
private void getContactsDetails(){
showLoader();
字符串选择=Contacts contract.Contacts.IN_VISIBLE_GROUP+“=”“
+ ("1") + "'";
游标phones=getActivity().getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
null、null/*选择+”和“+
Contacts contract.Contacts.HAS_PHONE_NUMBER+“=1”*/,null,“上限(”
+Contacts contract.Contacts.DISPLAY_NAME+”)ASC“;
ContactString=新的ArrayList();
如果(电话!=null){
如果(phones.getCount()>0){
tvNoContact.setVisibility(View.GONE);
while(phones.moveToNext()){
字符串名称=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_Name));
字符串编号=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.number));
HashSet-MobileSet=新HashSet();
如果(!mobileNoSet.contains(编号)){
字符串s=number.replaceAll(\\W,“”);
字符串lastTenCharContact=null;
如果(s!=null&&s.length()>10){
lastTenCharContact=s.substring(s.length()-10);
}否则{
lastTenCharContact=s;
}
//String substring=s.substring(Math.max(s.length()-10,0));
Log.d(标签“getContactsDetails:+lastTenCharContact”);
String image_uri=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.PHOTO_uri));
ContactString.add(lastTenCharContact);
DeviceContact contactModel=新的DeviceContact(Name.toUpperCase(),lastTenCharContact,image_uri);
contactModelsList.add(contactModel);
mobileNoSet.add(编号);
Log.d(标签,“名称:“+Name+”,编号:“+Number+”,照片:“+image_uri);}
}
hideLoader();
AllContactsyncapiCall();
}否则{
tvNoContact.setVisibility(View.VISIBLE);
}
} }
要删除重复编号,只需在循环之前设置哈希:
HashSet<String> mobileNoSet = new HashSet<String>();
这是一个新代码,其中我得到了重复的编号:-
private void getContactsDetails() {
// showLoader();
Cursor phones = 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<>();
nameString = new ArrayList<>();
if (phones != null) {
if (phones.getCount() > 0) {
tvNoContact.setVisibility(View.GONE);
HashSet<String> mobileNoSet = new HashSet<String>();
while (phones.moveToNext()) {
String Name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (!mobileNoSet.contains(number)) {
// String s = number.replaceAll("\\W", "");
String s = number.replaceAll("[^[+]\\d]", "");
String lastTenCharContact = null;
lastTenCharContact = s;
/*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);
// nameString.add(Name);
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);
}
}
}
private void getContactsDetails(){
//showLoader();
游标phones=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT_URI,null,null/*选择+”和“+ContactsContract.Contacts.HAS_Phone_NUMBER+”=1“*/,null”,大写(“+ContactsContract.Contacts.DISPLAY_NAME+”)ASC);
ContactString=新的ArrayList();
nameString=newArrayList();
如果(电话!=null){
如果(phones.getCount()>0){
tvNoContact.setVisibility(View.GONE);
HashSet-MobileSet=新HashSet();
while(phones.moveToNext()){
字符串名称=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_Name));
字符串编号=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.number));
如果(!mobileNoSet.contains(编号)){
//字符串s=number.replaceAll(\\W,“”);
字符串s=number.replaceAll(“[^[+]\\d]”,即“”);
字符串lastTenCharContact=null;
lastTenCharContact=s;
/*如果(s!=null&&s.length()>10){
lastTenCharContact=s.substring(s.length()-10);
}否则{
lastTenCharContact=s;
}*/
//String substring=s.substring(Math.max(s.length()-10,0));
Log.d(标签“getContactsDetails:+lastTenCharContact”);
String image_uri=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataTypes.Phone.PHOTO_uri));
ContactString.add(lastTenCharContact);
//nameString.add(Name);
contactModel=new DeviceContact(Name.toUpperCase(),lastTenCharContact,image_uri);
contactModelsList.add(contactModel);
mobileNoSet.add(编号);
Log.d(标签,“名称:“+Name+”,编号:“+Number+”,照片:“+image\u uri”);
}
}
//hideLoader();
AllContactsyncapiCall();
}否则{
tvNoContact.setVisibility(View.VISIBLE);
}
}
}
我在我的应用程序中使用了它,它工作得非常好。MobileNet hashSet存储数字,如果从游标设置contais数字,则不允许将其添加到DeviceContact contactModel中。发布更新的代码您将获得重复的条目,因为您正在每个循环中创建新的哈希集。现在移动这行HashSet mobilenset=newhashset();在tvNoContact.setVisibility(View.GONE)之后;thanx..它工作正常,但当我的设备与whatsapp或除我以外的任何其他帐户同步时,会出现相同的问题。如何与其他帐户同步?如果该联系人注册到该应用程序,则会自动从设备同步联系人。因此,如果我们在设备中看到该时间显示与whatsapp同步,mobiquick等。我想在contactModel=new DeviceContact(Name.toUpperCase(),lastTenCharContact,image_uri)中保存不同的联系人号码;
private void getContactsDetails() {
// showLoader();
Cursor phones = 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<>();
nameString = new ArrayList<>();
if (phones != null) {
if (phones.getCount() > 0) {
tvNoContact.setVisibility(View.GONE);
HashSet<String> mobileNoSet = new HashSet<String>();
while (phones.moveToNext()) {
String Name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (!mobileNoSet.contains(number)) {
// String s = number.replaceAll("\\W", "");
String s = number.replaceAll("[^[+]\\d]", "");
String lastTenCharContact = null;
lastTenCharContact = s;
/*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);
// nameString.add(Name);
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);
}
}
}