skip to Main Content

I am trying do I return the result of the position from my Recyclerview Adapter, but I can’t call the " adapter.setOnItemClickListener(this)" from MainActivity.kt? I get the error "Unresolved reference: setOnItemClickListener"?

I’ve had to post a new thread as I can’t get all the code to be shown – after taking quite a few hours failing on this I am losing the will to live 🙁

Adapter.kt

  class UsersAdapter(
    private val users: ArrayList<User>
    ) : RecyclerView.Adapter<UsersAdapter.DataViewHolder>() {
  class DataViewHolder(itemView: View) : 
          RecyclerView.ViewHolder(itemView) {
    fun bind(user: User) {
        itemView.textViewUserName.text = user.name
        Glide.with(itemView.imageViewAvatar.context)
            .load(user.avatar)
                .into(itemView.imageViewAvatar)
    }
 }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        DataViewHolder(
                LayoutInflater.from(parent.context).inflate(
                        R.layout.item_layout, parent, false)             
        )
override fun getItemCount(): Int = Int.MAX_VALUE
override fun onBindViewHolder(holder: DataViewHolder, position: Int){ 
    val pos = position % users.size
    holder.bind(users[pos])        
    Log.d(Constraints.TAG, "onBindViewHolder:" + pos)
}
lateinit var listener: OnItemClickListener
public interface OnItemClickListener {
    fun getAdapterPosition(position : Int )
}
public fun setOnItemClickListener(listener: OnItemClickListener) {
    this.listener= listener
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
lateinit var adapter: ConcatAdapter
lateinit var userVerticalAdapter: UsersAdapter
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setupDataInRecyclerView()
}
//override
public fun getAdapterPosition(position : Int ){
    // required value is in the position variable
    Log.d(Constraints.TAG, "This is where it is going!" )
}
private fun setupDataInRecyclerView() {
    recyclerView.layoutManager = LinearLayoutManager(this, 
              LinearLayoutManager.HORIZONTAL, false)
    userVerticalAdapter = UsersAdapter(DataSource.getUser())
    val listOfAdapters = listOf(userVerticalAdapter)
    adapter = ConcatAdapter(listOfAdapters)
    //This errors with "Unresolved reference: setOnItemClickListener"
    adapter.setOnItemClickListener(this)
    recyclerView.adapter = adapter
    recyclerView.scrollToPosition(Int.MAX_VALUE/2)
    ItemSnapHelper().attachToRecyclerView(recyclerView)
}
public fun ShowWhatRecieved(isitthere: Int){
    ShowMeIt.text = isitthere.toString()}
}

Revised and Updated MainActvity.kt

class MainActivity : AppCompatActivity(), UsersAdapter.OnItemClickListener {
lateinit var adapter: ConcatAdapter
lateinit var userVerticalAdapter: UsersAdapter
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setupDataInRecyclerView()
}

override public fun getAdapterPosition(position : Int ){
    Log.d(Constraints.TAG, "This is where it is going!" )
}

private fun setupDataInRecyclerView() {
    recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
    userVerticalAdapter = UsersAdapter(DataSource.getUser())
    val listOfAdapters = listOf(userVerticalAdapter)
    adapter = ConcatAdapter(listOfAdapters)
    adapter.setOnItemClickListener(this)
    recyclerView.adapter = adapter
    recyclerView.scrollToPosition(Int.MAX_VALUE/2)
    ItemSnapHelper().attachToRecyclerView(recyclerView)
}
public fun ShowWhatRecieved(isitthere: Int){
    ShowMeIt.text = isitthere.toString()
}
}

4

Answers


  1. You are passing this as argument it will pass the context but in the parameter of function setOnItemClickListener in adapter you have defined listener of OnItemClickListener type. So you need to pass the argument of OnItemClickListener type.

    Login or Signup to reply.
  2. You should implement the OnItemClickListener at your activity and you will be able to use this keyword. But you also can make an anonymous implementation of OnItemClickListener interface when you try to send.

    But I would prefer the option 1. To implement the OnItemClickListener to your activity, implement all methods of that interface and than you can use this keyword when you need to pass OnItemClickListener anywhere in that class.

    Login or Signup to reply.
  3. You have not implemented interface in your as below –

    class MainActivity : AppCompatActivity(), RecycleViewCartAdapter.OnItemClickListener 
    

    Here is the problem mate.

    Login or Signup to reply.
  4. Despite all your effort by creating an interface and implementing it inside your recycler-view, I suggest you use lambda instead of interface since you are writing Kotlin, and writing a lambda instead of an interface is a cleaner approach in Kotlin for ClickListener.

    Create a lambda parameter both for your adapter and your viewholder. since you want to pass the position of a clicked item so your lambda must have an Int input and nothing as output so it would look like this: listener: (Int) -> Unit.

    The rest of your code would be something like this, you get the idea I just edited important parts:

    class UsersAdapter(private val users : ArrayList<CrashlyticsReport.Session.User>,
                       private val listener : (Int) -> Unit) :
        RecyclerView.Adapter<UsersAdapter.DataViewHolder>() {
        
        class DataViewHolder(itemView : View, private val listener : (Int) -> Unit) :
            RecyclerView.ViewHolder(itemView) {
            
            init {
                itemView.setOnClickListener { listener(adapterPosition) }
            }
            
            fun bind(user : CrashlyticsReport.Session.User) {
                itemView.textViewUserName.text = user.name
                Glide.with(itemView.imageViewAvatar.context).load(user.avatar).into(itemView.imageViewAvatar)
            }
        }
    }
    

    then in your activity you can write:

    userVerticalAdapter = UsersAdapter(DataSource.getUser()) {
    //clicklistener event here
    }
    

    or:

    userVerticalAdapter = UsersAdapter(DataSource.getUser(),this::ShowWhatRecieved)
    

    also since you’re using ConcatAdapter it’s better to use bindingAdapterPosition instead of adapterPosition.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search