0

I have working Kotlin code that displays a subset of contacts from a search criteria. Now I want to let the users press a specific contact in that result to display that contact's details. There are several examples on how to display the contact details in stackoverflow. For example: Pick contact directly from contact picker intent. But it's unclear where to insert this additional feature into the code.

I tried adding in the OnCreate section

    i = Intent(Intent.ACTION_PICK)
     i.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
            startActivityForResult(i, SELECT_PHONE_NUMBER) 

and later on in the code

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
                super.onActivityResult(requestCode, resultCode, data)
                if (requestCode == SELECT_PHONE_NUMBER && resultCode == Activity.RESULT_OK) {
                    val contactUri = data?.data ?: return
                    val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER)
                    val cursor = requireContext().contentResolver.query(contactUri, projection,
                        null, null, null)
        
                    if (cursor != null && cursor.moveToFirst()) {
                        val nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
                        val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
                        val name = cursor.getString(nameIndex)
                        val number = cursor.getString(numberIndex)
        
                        // do something with name and phone
                    }
                    cursor?.close()
                }
            }

But I got an error message ConnectActivity.kt: No value passed for parameter 'p0' for the line in the code that says val cursor = requirecontext().contentResolver.query.....specifically in the() area. If you can show how to fix this or provide a different solution, it would be greatly appreaciated.

The working code without the changes discussed above is here:

    package com.example.myapplication
    
    import android.Manifest
    import android.app.Activity
    import android.content.Intent
    import android.content.pm.PackageManager
    import android.os.Bundle
    import android.provider.ContactsContract
    import android.widget.ListView
    import androidx.appcompat.widget.SearchView
    
    import android.widget.SimpleCursorAdapter
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.core.app.ActivityCompat
    import androidx.core.content.ContentProviderCompat.requireContext
    import java.io.IOException
    
    class ConnectActivity : AppCompatActivity() {
        private lateinit var searchView: SearchView
    
        private lateinit var listView: ListView
        var cols = listOf<String> (
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone._ID
                ).toTypedArray()
    
     
            try {
                //Toast.makeText(getApplicationContext(), "got here", Toast.LENGTH_SHORT).show();
                searchView = findViewById(R.id.searchView)
                listView = findViewById(R.id.listView)
                if (ActivityCompat.checkSelfPermission( this@ConnectActivity, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this@ConnectActivity, Array(1){Manifest.permission.READ_CONTACTS}, 111)
                }
                else {
                    readContact()
                }
    
            } catch (e: Exception) {
                Toast.makeText(getApplicationContext(), e.message, Toast.LENGTH_LONG).show()
            }
    
    
        }
        override fun onRequestPermissionsResult(requestCode: Int,permissions: Array<out String>,grantResults: IntArray){
            super.onRequestPermissionsResult(requestCode,permissions,grantResults)
            Toast.makeText(getApplicationContext(), "then here", Toast.LENGTH_SHORT).show()
            if (requestCode==111 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
               readContact()
        }
    
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (requestCode == SELECT_PHONE_NUMBER && resultCode == Activity.RESULT_OK) {
                val contactUri = data?.data ?: return
                val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER)
                val cursor = requireContext().contentResolver.query(contactUri, projection,
                    null, null, null)
    
                if (cursor != null && cursor.moveToFirst()) {
                    val nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
                    val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
                    val name = cursor.getString(nameIndex)
                    val number = cursor.getString(numberIndex)
    
                    // do something with name and phone
                }
                cursor?.close()
            }
        }
        private fun readContact() {
    
    
            var from = listOf<String>(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER).toTypedArray()
    
            var to = intArrayOf(android.R.id.text1,android.R.id.text2)
    
            var rs = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                           cols,null,null,
                                           ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
            var adapter = SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,
                                           rs,from,to,0)
            listView.adapter =adapter
    
    
            searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
                override fun onQueryTextSubmit(p0: String?):Boolean {
                    return false
                }
                override fun onQueryTextChange(p0:String?):Boolean{
                     rs = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        cols,"${ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME} LIKE ?",
                        Array(1){"%$p0%"},
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
                     adapter.changeCursor(rs)
                    return false
                }
            })
    
    
        }
        companion object {
    
            private const val SELECT_PHONE_NUMBER = 111
    
        }
    
    }

xml file is

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">
    
    
        <androidx.appcompat.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="20dp"
            android:queryHint="hi"
            />
        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/searchView"
        />
    </RelativeLayout>
Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
quinn
  • 177
  • 1
  • 2
  • 13

0 Answers0