Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Get contacts in Android without duplicates

I've got the following code to retrieve a contact list with Name + PhoneNumber:

    @SuppressLint("Range")
    
    fun getNamePhoneDetails(): ArrayList<List<String>>? {
        val names = ArrayList<List<String>>()
        val cr = contentResolver
        val cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            null, null, null)
        if (cur!!.count > 0) {
            while (cur.moveToNext()) {
                val id = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NAME_RAW_CONTACT_ID))
                val name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                val number = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                names.add(listOf(id, name, number))
            }
        }
        return names
    }

The output is correct once no contact has two phone numbers. However once any contact has two phone numbers I get the contact twice. It has the same id and name, but a different phone number. How can I make it so that the returned list does not have a contact twice but all phone numbers inside a list?

Something like

[1, Name Name, [phonenumber1, phonenumber2]],[2, Name Name, [phonenumber1]]

Then I could just iterate through the phonenumberlist and have all numbers as valid strings.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You should rather create a POJO Object and than assign the data into it and return a list of that POJO Object and then you can make use of distinct extension on the list to get a filtered result they way you want it . This is how you can achieve what you want :

Create a POJO Object :

data class Contact(
val id : String ,
val name : String, 
val number : String)

And then when retrieveing the data you can do the following :

   @SuppressLint("Range")
    
    fun getNamePhoneDetails(): MutableList<Contact>? {
        val names = MutableList<Contact>()
        val cr = contentResolver
        val cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            null, null, null)
        if (cur!!.count > 0) {
            while (cur.moveToNext()) {
                val id = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NAME_RAW_CONTACT_ID))
                val name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                val number = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                names.add(Contact(id , name , number))
            }
        }
        return names
    }

And then while retreiving the data you need to just filter the list in the following manner :

 val list = getNamePhoneDetails()
 list.distinctBy { it.number }.forEach { println(it) }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!