3

I started android development a few days ago. I implemented a recylerview and in the OnBindViewHolder method of the recyclerview adapter I used the setOnClickListener on the recyclerview item. My main goal was to start a new activity when the recyclerview item is clicked but I ran into a wall when implementing my code in the following way:

     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val clusterItem = datalist[position]
    holder.clusterName.setText(clusterItem.name)
    holder.clusterStrat.setText(clusterItem.strats)
    holder.itemview.setOnClickListener() { 
        startActivity(Intent(holder.itemview.context,ClusterSearchActivity::class.java))
    }
}

I had 3 errors on the line that contains startActivity:

Type mismatch: inferred type is Intent but Context was expected

No value passed for parameter 'intent'

No value passed for parameter 'options'

After going through multiple solutions I finally stumbled upon this one: https://www.titanwolf.org/Network/q/08ad14d9-cb9a-4b87-923b-f97089db769a/y

Using context.startActivity(intent) I rewrote my code like this:

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         val clusterItem = datalist[position]
         holder.clusterName.setText(clusterItem.name)
         holder.clusterStrat.setText(clusterItem.strats)
         holder.itemview.setOnClickListener() {
    holder.itemview.context.startActivity(Intent(holder.itemview.context,ClusterSearchActivity::class.java)) }

}

Now my code finally worked but I can't seem to understand why I had to use context.startActivity(). I'd like to understand when can I use startActivity() just like that and when I need to use context.startActivity().

  • 1
    Not much there to understand if you know basics pf OOPS . `startActivity` is a non-static method of some class and to call it you need an Object if that class . Its a method of `Context` that's why you need context to call it Since Activity is a child of Context so you can directly use it in Activity . – ADM Dec 31 '21 at 09:59
  • 1
    An `Activity` itself is a `Context` so you don't need to use `context...`, whereas the RecyclerView or its Adapter are not so you need to get access to a valid `Context` via the inflated view (`itemView.context`). – Darshan Dec 31 '21 at 10:25

2 Answers2

1

That's because the method startActivity() is a member of the Activity class and not RecyclerView, it simply does not exist there.

Incidentally, a better way that you can start an Activity from a RecyclerView click is via a callback.

Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34
1
  1. First of all, Context is the current state of the application/object.

  2. Interface to global information about an application environment.

  3. This is an abstract class whose implementation is provided by the Android system.

  4. It allows access to application-specific resources like colors, string resource , database access and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

  5. Context is the base class for Activity, Service, Application, etc if you check inside the AppCompatActivity and Fragment. then you can be found startActivity() method inside it.

In Your case:

In the adapter, If you need to get the database access , string res e.g: context.getResources().getString(R.string.yourstring);

needed color to set on the view on runtime , so you have to need the current state of the application/object is call context and context is a super class.

You can get access to context in three ways in the adapter.

  1. Pass Context as an argument to the Adapter and keep it as class field.

  2. Use dependency injection to inject Context when you need it. I strongly suggest reading about it.e.g: Android Hilt.

At last,

  1. Get it from any View object. just like you did it. holder.itemview.context.

I hope, it might be helpful for you.

Muhammad Ammar
  • 476
  • 3
  • 9
  • Does it matter which view I use to get the context? – Sarwar Khalid Dec 31 '21 at 11:38
  • 1
    A/c to the Google Developer Documentation: A ViewHolder describes an item view and metadata about its place within the RecyclerView. The View class is the base class or we can say that it is the superclass for all the GUI components in Android. Eg: EditText, Button, CheckBox, etc extend from view. public ViewHolder (View itemView) Each ViewHolder contains an instance of itemView that you pass in the constructor. So itemView is also a view and this approach holder.itemView.editText.context is a View too. so its doesn't matter where you get the context. – Muhammad Ammar Dec 31 '21 at 13:53