Android get phone contacts and remove duplicates

后端 未结 5 1982
天命终不由人
天命终不由人 2021-01-14 11:53

I am having an issue related to contacts. I got the phone contacts and stored them in my list object. Here\'s the code for it

  Uri uri = ContactsContract.Da         


        
5条回答
  •  借酒劲吻你
    2021-01-14 12:33

    Use PhoneNumberUtils.compare(a, b) to filter out duplicated numbers

    val contacts = ArrayList()
    val uniqueMobilePhones = ArrayList()
                    while (cursorPhones.moveToNext()) {
                        val displayName = cursorPhones.getString(cursorPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                        val number = cursorPhones.getString(cursorPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                        val convertedNumber = convert(telman, number)
                        var duplicate = false
                        uniqueMobilePhones.forEach { addedNumber ->
                            if (PhoneNumberUtils.compare(addedNumber, number)) {
                                duplicate = true
                            }
                        }
    
                        if (!duplicate) {
                            uniqueMobilePhones.add(number)
                            contacts.add(MyContact(displayName, number, convertedNumber.replace(Regex("[ -+()]"), "")))
                        }
                    }
    

提交回复
热议问题