Home > database >  Using onClickListener in Recycler View with Retrofit Structure - Kotlin
Using onClickListener in Recycler View with Retrofit Structure - Kotlin

Time:05-04

Hello i have a recyclerview that is filled with some data coming from a Webservice, using Retrofit.

Now I want to implement a onClickListener, so when i click each row of the Recycler View, i can see more data from that object, and tried to work with some examples, but i got stucked

Here is my adapter. I know that in the onCreateViewHolder, i should put in the Return AnunciosViewHolder a second parameter, of the type cellClickListener, but i have no idea what i have to put. I tried this@CellCLickListener and this@cellCLickListener and it gave me error that is is unresolved

class AnuncioAdapter(val anuncios: List<Anuncio>): RecyclerView.Adapter<AnunciosViewHolder>() {



        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnunciosViewHolder {

            val view = LayoutInflater.from(parent.context).inflate(R.layout.recyclerline, parent, false)
            return AnunciosViewHolder(view)
        }

        override fun getItemCount(): Int {
            return anuncios.size
        }

        override fun onBindViewHolder(holder: AnunciosViewHolder, position: Int) {
            return holder.bind(anuncios[position])
        }


    }
    class AnunciosViewHolder(itemView : View,  private val cellClickListener: CellClickListener): RecyclerView.ViewHolder(itemView){
        private val morada: TextView = itemView.findViewById(R.id.morada)
        private val telemovel: TextView = itemView.findViewById(R.id.number)
        private val fotografia: ImageView = itemView.findViewById(R.id.image)



        fun bind(anuncio: Anuncio) {
            morada.text = anuncio.morada
            telemovel.text = anuncio.telemovel

            itemView.setOnClickListener {
                cellClickListener.onCellClickListener(anuncio)
            }



I also tried creating an interface

interface CellClickListener {
    fun onCellClickListener (data: Anuncio)
}

and in my Activity i put this method and it gives me an error that «overrides nothing»

override fun onCellClickListener(data: Anuncio) {
        val intent = Intent(this@ListaAnuncios, DetalhesActivity::class.java)
        intent.putExtra(PARAM_ID, data.id.toString())
        intent.putExtra(PARAM_MORADA, data.morada)
           intent.putExtra(PARAM_TELEMOVEL, data.telemovel)
        startActivityForResult(intent, newAnuncioActivityRequestCode1)
        Log.e("***ID", data.id.toString())
    }

UPDATE

After using the suggestions made by Praveen i was able to clean my Adapter from errors, however i am struggling in the activity part

if it put

val anuncioAdapter = AnuncioAdapter(anuncios, this)

on the beggining of my On Create, it doesn't recognize «anuncios»

However i am declaring my adapter inside the call.enqueue

 recyclerView.apply {
                            setHasFixedSize(true)
                            layoutManager = 
                             LinearLayoutManager(this@ListaAnuncios)
                            adapter = AnuncioAdapter(response.body()!!)

                        }

And it is asking to pass an instance of cellClickListener here, but if i use «this» in here, it is stated that i am trying to pass an instance of the recycler view instead of the CellClickListener

NEW UPDATE

Forgot to put all the call.enqueue method

 call.enqueue(object : Callback<List<Anuncio>> {
            override fun onResponse(call: Call<List<Anuncio>>, response: Response<List<Anuncio>>) {
                if (response.isSuccessful){



                      recyclerView.apply {
                            setHasFixedSize(true)
                            layoutManager = 
                           LinearLayoutManager(this@ListaAnuncios)
                            adapter = AnuncioAdapter(response.body()!!)

                        }

                }
            }
            override fun onFailure(call: Call<List<Anuncio>>, t: Throwable) {
                Toast.makeText(this@ListaAnuncios, "${t.message}", Toast.LENGTH_LONG).show()
            }
        }) }

i tried both approaches of @Praveen and @aligur, but still struggling with asking me to put the instance of Clicklistener as the 2nd parameter, but using «this» is putting the instance of the Recycler View and not of the ClickListener

Thank You in advance

CodePudding user response:

and in my Activity i put this method and it gives me an error that «overrides nothing»

You are not implementing CellClickListener in your activity. Add CellClickListener after your activity's class name declaration

class MainActivity : AppCompatActivity(), CellClickListener {

}

I know that in the onCreateViewHolder, i should put in the Return AnunciosViewHolder a second parameter, of the type cellClickListener, but i have no idea what i have to put. I tried this@CellCLickListener and this@cellCLickListener and it gave me error that is is unresolved

You've to add the private val cellClickListener: CellClickListener parameter to the constructor of AnuncioAdapter, not the ViewHolder. Only then you will be able to pass it from your activity.

Change constructor of AnuncioAdapter to accept a CellClickListener and remove the same from the constructor of AnunciosViewHolder

class AnuncioAdapter(
    private val anuncios: List<Anuncio>, 
    private val cellClickListener: CellClickListener
): RecyclerView.Adapter<AnunciosViewHolder>() {

}

To access this cellClickListener inside AnunciosViewHolder you've to make it an inner class of AnuncioAdapter, which you can make, as it's already tightly coupled with the adapter.

inner class AnunciosViewHolder(itemView : View): RecyclerView.ViewHolder(itemView){

}

Now, on creating an object of AnuncioAdapter inside activity, just pass an instance of cellClickListener using this, as it's already implementing it.

val anuncioAdapter = AnuncioAdapter(anuncios, this)

CodePudding user response:

I think the easiest way is passing function as parameter to RecyclerViewAdapter.

for instance:

RecyclerViewAdapter(val clickListener : () -> Unit)

onCreateViewHolder(){
clickListener.invoke()
}

in your view

adapter = ReceylerViewAdapter({
//do your stuff here
})
  • Related