Home > Software engineering >  [KOTLIN]I am trying to pass data from my recycler view to another activity but don't know how
[KOTLIN]I am trying to pass data from my recycler view to another activity but don't know how

Time:01-25

i am trying to get the Movieid from my current recycler view to another activity i got the data from another api but to display the details about the movie i need the movieid but i am unable to pass the data

this is my main class

package com.example.login

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.login.Tmdb.moviesIns
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response



class Real_home : Fragment(){
    private lateinit var adapter: Adapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_real_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        getData()
    }

    @SuppressLint("SuspiciousIndentation")
    private fun getData() {
 val movies = moviesIns.getMovies(1)
       movies.enqueue(object : Callback<Movie> {
           override fun onResponse(call: Call<Movie>, response: Response<Movie>) {
               val movie66 = response.body()
               if (movie66!=null)
               Log.d("hello", response.toString())
               adapter= Adapter(this@Real_home, movie66!!.results)

               val Lemmetry= view!!.findViewById<RecyclerView>(R.id.Lemmetry)
               Lemmetry.adapter=adapter
           //    var layoutManager: Lemmetry.LayoutManager = LinearLayoutManager(context)
              Lemmetry.layoutManager= LinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false)
             adapter.onItemClick= {
                 val intent = Intent(activity,show_Movies::class.java).apply {
                 }
                 startActivity(intent)
             }



           }

           override fun onFailure(call: Call<Movie>, t: Throwable) {
               Log.d("hello", "onFailure: Something went wrong")
           }

       })

    }/*
    private fun showMovieDetails(movie: Movie) {
        val intent = Intent(this, show_Movies::class.java)

        intent.putExtra(MOVIE_BACKDROP,com.example.login.Result.backdropPath)
        intent.putExtra(MOVIE_POSTER, movie.posterPath)
        intent.putExtra(MOVIE_TITLE, movie.title)
        intent.putExtra(MOVIE_RATING, movie.rating)
        intent.putExtra(MOVIE_RELEASE_DATE, movie.releaseDate)
        intent.putExtra(MOVIE_OVERVIEW, movie.overview)
        startActivity(intent)*/

    private fun ino(result: com.example.login.Result) {

    }
}




this is my adapter class

package com.example.login

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class Adapter(val context: Real_home, val MovieList: List<Result>) : RecyclerView.Adapter<Adapter.ViewHolder>() {
    var onItemClick:((show_Movies)->Unit)? = null


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflate= LayoutInflater.from(parent.context)
        val view = inflate.inflate(R.layout.card,parent,false)
        return ViewHolder(view)
    }

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

    override fun onBindViewHolder(holder:ViewHolder, position: Int) {
val realMovies= MovieList[position]
      //  holder.MovieName.text=realMovies.original_title
       // holder.des.text=realMovies.overview
        Glide.with(context).load("https://image.tmdb.org/t/p/w500"  realMovies.poster_path).into(holder.Pics)
           // holder.pop.text=realMovies.popularity
        holder.itemView.setOnClickListener {
 onItemClick?.invoke(show_Movies())
        }


    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
      //var MovieName=  itemView.findViewById<TextView>(R.id.movieName)
        var Pics= itemView.findViewById<ImageView>(R.id.pics)
      //  var des = itemView.findViewById<TextView>(R.id.des)
       // var pop = itemView.findViewById<TextView>(R.id.pop)

    }
}


this is the class where i want the data

package com.example.login

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class show_Movies : AppCompatActivity() {

    @SuppressLint("MissingInflatedId", "CheckResult")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_show_movies)

        val extras = intent.extras


    }
}

i am kinda new in programming

CodePudding user response:

Your OnItemClick lambda is wrong

Assuming the Id of the movie is Movie.Id and is a String In your Adapter class add a String argument as follow :

var onItemClick:((String)->Unit)? = null

Invoke the lambdas in Adapter with the movieId in argument :

holder.itemView.setOnClickListener {
      onItemClick?.invoke(realMovie.Id)
    }

And then you can give this onItemClick to your Adapter class

adapter.onItemClick= { movieId ->
   val intent = Intent(activity,show_Movies::class.java).apply {
          putExtra(movieId, "movie_id")
       }
    startActivity(intent)
  }

And you can get your movieId String in the other activity via the intent :

class show_Movies : AppCompatActivity() {

@SuppressLint("MissingInflatedId", "CheckResult")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_show_movies)

    val movieId = intent.getStringExtra("movie_id")

   }
}

If Id is an int adapt the argument of the lambda.

  • Related