title: Read contacts and display it in a recyclerview in Kotlin
body: <p>I am trying to read contacts from ContactsContract in Kotin. But <strong>it's not showing any of the contact in the recyclerview</strong>. The noticeable thing is that the <strong><em>java version of this code is working fine</em></strong>.</p>
  
  <p>So, I here is my Kotlin code for reading the contact:</p>
  
  <pre><code>private var adapter: ContactsAdapter? = null
  private var myContacts : MutableList&lt;MyContacts&gt; = ArrayList()
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
  
      // code for recyclerview and adapter and checkPermission
      val itemDecoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
      recycler_view.layoutManager = LinearLayoutManager(this)
      recycler_view.addItemDecoration(itemDecoration)
      adapter = ContactsAdapter(myContacts)
      recycler_view.hasFixedSize()
      recycler_view.adapter = ContactsAdapter(myContacts)
  
      checkPermission()
  }
  
  private fun loadContactFromProvider() {
      showProgressBar()
      val contentResolver = contentResolver
      val cursor = contentResolver.query(CONTENT_URI, null, null, null, DISPLAY_NAME)
  
      if (cursor != null &amp;&amp; cursor.count &gt; 0) {
          while (cursor.moveToNext()) {
              val id = cursor.getString(cursor.getColumnIndex(ID))
              val name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME))
              val hasPhoneNumber = cursor.getInt(cursor.getColumnIndex(HAS_PHONE_NUMBER))
              val contacts = MyContacts()
  
              if (hasPhoneNumber &gt; 0) {
                  contacts.contactName = name
                  val phoneCursor = contentResolver.query(PHONE_URI, arrayOf(NUMBER), "$PHONE_ID = ?", arrayOf(id), null)
                  val phoneNumbers = ArrayList&lt;String&gt;()
                  phoneCursor!!.moveToFirst()
                  while (!phoneCursor.isAfterLast) {
                      val phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)).replace(" ", "")
                      phoneNumbers.add(phoneNumber)
                      phoneCursor.moveToNext()
                  }
                  contacts.contactNumber = phoneNumbers
                  phoneCursor.close()
              }
              val inputStream = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, ContentUris.withAppendedId(CONTENT_URI, id.toLong()))
              if (inputStream != null) {
                  val bitmap = BitmapFactory.decodeStream(inputStream)
                  contacts.contactImage = bitmap?.toString()
              } else {
                  contacts.contactImage = vectorDrawableToBitmap(R.drawable.ic_person)?.toString()
              }
  
              log(contacts.contactName + " " + contacts.contactNumber.toString() + " " + contacts.contactImage.toString())
              myContacts.add(contacts)
          }
          adapter?.notifyDataSetChanged()
          cursor.close()
      }
  }
  </code></pre>
  
  <p>The log details are also fine, they are showing the full contact list. But I am unable to see it in the recyclerview. The constant field like ID, DISPLAY_NAME etc. are already defined in the companion object.</p>
  
  <p>Kotlin code for RecyclerViewAdapter is:</p>
  
  <pre><code>class ContactsAdapter(private val contactList: MutableList&lt;MyContacts&gt;): RecyclerView.Adapter&lt;ContactsAdapter.ContactViewHolder&gt;() {
  
  override fun onCreateViewHolder(viewGroup: ViewGroup, position: Int): ContactViewHolder {
      val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.contact_item_layout,viewGroup,false)
      return ContactViewHolder(view)
  }
  
  override fun onBindViewHolder(holder: ContactViewHolder, position: Int) {
      val contact = contactList[position]
      holder.name!!.text = contact.contactName
      holder.mobile!!.text = contact.contactNumber[0]
      Glide.with(holder.itemView.context)
              .load(contact.contactImage)
              .into(holder.image)
  }
  
  override fun getItemCount(): Int = contactList.size
  
  class ContactViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
      var image : ImageView = itemView.find(R.id.contact_image) as ImageView
      var name : TextView? = itemView.find(R.id.contact_name) as TextView
      var mobile : TextView? = itemView.find(R.id.contact_mobile) as TextView
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thankyou</p>