title: Returning the location from the adapter back to the mainactivity in Kotlin?
body: <p>I have a RecyclerView and am trying to pass the location from my adapter back to the mainactivity without success so far .</p>
  <p>I think the code in the adapter is passing the data back is working, but unfortunately I can’t work out how to set the initialisation in the onCreate in the MainActivity to work? I’m not certain how to call this using “<em>lazy</em>” (ie “private val itemAdapter by lazy” in the mainactivity”)?</p>
  <p>I am getting frustrated solving this so any help appreciated.</p>
  <p><strong>MainActivity.kt</strong></p>
  <pre><code>class MainActivity : AppCompatActivity(), ItemAdapter.OnItemClickListener {
  private val itemAdapter by lazy {
      ItemAdapter { position: Int, item: Item -&gt;
          Toast.makeText(this@MainActivity, &quot;Pos ${position}&quot;, Toast.LENGTH_LONG).show()
          item_list.smoothScrollToPosition(position)
      } }
  private val possibleItems = listOf(
      Item(&quot;Plane 1&quot;, R.drawable.ic_airplane),
      Item(&quot;Car 2&quot;, R.drawable.ic_car),
      Item(&quot;Meals 3&quot;, R.drawable.ic_food),
      Item(&quot;Fuel 4&quot;, R.drawable.ic_gas),
      Item(&quot;Home 5 &quot;, R.drawable.ic_home)
  )
  fun onImageClick(imageData: String?) {
      // handle image data
  }
  override fun onCreate(savedInstanceState: Bundle?) {
      var position: Int
      val thisitem: Item
      var thisunit: Unit
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      //       var itemAdapter = ItemAdapter(possibleItems)
      //       var thisitemAdapter = ItemAdapter((position, thisitem) -&gt; thisunit)
      //          Toast.makeText(this@MainActivity, &quot;Pos ${position}&quot;, Toast.LENGTH_LONG).show()
      //          item_list.smoothScrollToPosition(position) //CAN'T GET THIS TO COMPILE 
      item_list.initialize(itemAdapter)
      itemAdapter.setOnItemClickListener(this) // Added this
      ItemSnapHelper().attachToRecyclerView(item_list)
      item_list.setViewsToChangeColor(listOf(R.id.list_item_background, R.id.list_item_text))
      item_list.scrollToPosition(Int.MAX_VALUE/2)
      itemAdapter.setItems(possibleItems)
  }
  override
  public fun getAdapterPosition(position : Int ){
      Toast.makeText(this,&quot;this is toast message&quot; + position,Toast.LENGTH_SHORT).show()
  }
  private fun getLargeListOfItems(): List&lt;Item&gt; {
      val items = mutableListOf&lt;Item&gt;()
      (0..40).map { items.add(possibleItems.random()) }
      return items
  }
  }
  data class Item(
      val title: String,
      @DrawableRes val icon: Int
  )
  </code></pre>
  <p><strong>ItemAdapter.kt</strong></p>
  <pre><code>class ItemAdapter(val itemClick: (position:Int,item:Item) -&gt; Unit) : RecyclerView.Adapter&lt;ItemViewHolder&gt;() {
  private var items: List&lt;Item&gt; = listOf()
  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder =
      ItemViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false))
  override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
      val pos = position % items.size
      holder.bind(items[pos])        // bind the actual item
      Log.d(&quot;xx position= &quot;, pos.toString())
  }
  override fun getItemCount(): Int = Int.MAX_VALUE
  fun setItems(newItems: List&lt;Item&gt;) {
      items = newItems
      notifyDataSetChanged()
  }
  lateinit var listener: OnItemClickListener
  
  public interface OnItemClickListener {
      fun getAdapterPosition(position : Int )
  }
  public fun setOnItemClickListener(listener: OnItemClickListener) {
      this.listener= listener
  }
  }
  class ItemViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
      fun bind(item: Item) {
          view.list_item_text.text = &quot;${item.title}&quot;
          view.list_item_icon.setImageResource(item.icon)
       }
   }
  </code></pre>