Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

758
Visualizações
startActivity() vs context.startActivity()

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().

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

  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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda