简体   繁体   中英

How to get the id from the click item from the list?

I have a recycler view adapter and I pass there a list of movies. Each item has an id, poster_path, title, overview, genres, and runtime. I need to detect what item the user clicked on and pass its id, title, and other information in intent to another activity. It sounds not very difficult but for some reason, it doesn't work for me. Could you please help me?

class MoviesRecyclerAdapter() : RecyclerView.Adapter<MoviesRecyclerAdapter.RecyclerViewHolder>() {

    private var poster: String = "https://image.tmdb.org/t/p/w500"
    private var mylist = mutableListOf<MovieAndDetailsUi>()


    class RecyclerViewHolder(val binding: StarMovieRecycleItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        val binding =
            StarMovieRecycleItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return RecyclerViewHolder(binding);
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        val recyclerViewItem = mylist.get(position)

        with(holder) {
            with(mylist[position]) {

                //Log.d("test", mylist.toString())

                binding.posterIv.load(poster + recyclerViewItem.poster_path)

                binding.movieTitleTv.text = recyclerViewItem.title

                binding.genreTv.text = recyclerViewItem.genres.get(0).name

                binding.movieDurationTv.text = convertTime(mylist.get(position).runtime)

            }
        }

        holder.itemView.setOnClickListener {
            val id: String = holder.itemView.id.toString()

            val intent = Intent(holder.binding.root.context, DetailsActivity::class.java)
            intent.putExtra("id", id)
            //intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK //TODO add movie id to extras
            holder.binding.root.context.startActivity(intent)
        }

    }



    override fun getItemCount(): Int = mylist.size

    fun setSomeList(list: MutableList<MovieAndDetailsUi>) {
        mylist.addAll(list)
        notifyDataSetChanged()
    }

    fun convertTime(duration: Int):String{

        val hours = duration/60
        val min = duration % 60
        return String.format("%2dhr %02dm", hours, min)
    }

} 

Add an onClick function as an input parameter into the constructor of MoviesRecyclerAdapter like so:

class MoviesRecyclerAdapter(private val onItemClick: (MovieAndDetailsUi) -> Unit) : RecyclerView.Adapter<MoviesRecyclerAdapter.RecyclerViewHolder>() {
     ...
}

Now when you create your MoviesRecyclerAdapter instance, you can define a function to be ran whenever you click an item. You could do something like this:

val adapter = MoviesRecyclerAdapter({ item ->
     // On click functionality here
     // For example, you could do myApiRequest(item.id)
})

Then, in onBindViewHolder() in the adapter, you can change your click listener to call that function you set when constructing your adapter:

holder.itemView.setOnClickListener {
    onItemClick(recyclerViewItem)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM