PARAMETER FOR SORT ORDER

Asked

Viewed 15 times

-1

I’m taking a course and would like to order the return list by name. I tried inserting as DISPLAY_NAME parameter but does not return. Could help me know how to identify and what parameter to return in that order?

in that passage >>

val cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        null,
        null,
        null,
        null)

follows Cód >>

val REQUEST_CONTACT = 1
val LINEAR_LAYOUT_VERTICAL = 1

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_contact)

    if (ActivityCompat.checkSelfPermission(
            this,android.Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this,
        arrayOf(Manifest.permission.READ_CONTACTS), REQUEST_CONTACT)
    }else{
        setContacts()
    }
}

@SuppressLint("MissingSuperCall")
override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    if (requestCode == REQUEST_CONTACT) setContacts()
}

private fun setContacts() {
    val contactList: ArrayList<Contact> = ArrayList()


    val cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            null,
            null,
            null)


    if (cursor != null){
        while (cursor.moveToNext()){
            contactList.add(Contact(
                    cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),
                    cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))

            ))
        }
        cursor.close()
}
    val adapter = ContactsAdapter(contactList)
    val contactRecyclerView = findViewById<RecyclerView>(R.id.contacts_rv)

    contactRecyclerView.layoutManager= LinearLayoutManager(
            this,
            LINEAR_LAYOUT_VERTICAL,
            false
            )

    contactRecyclerView.adapter = adapter
}
  • Isn’t it easier to sort the contactList by the desired criteria yourself? Look for the Arraylist.sortedWith method().

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.