[英][KOTLIN]I am trying to pass data from my recycler view to another activity but don't know how
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我正在尝试将 Movieid 从我当前的回收者视图获取到另一个活动我从另一个 api 获得了数据但是要显示有关电影的详细信息我需要 movieid 但我无法传递数据
this is my main class这是我的主 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这是我的适配器 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这是我想要数据的 class
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我是编程新手
Your OnItemClick
lambda is wrong你的
OnItemClick
lambda 是错误的
Assuming the Id of the movie is Movie.Id
and is a String
In your Adapter class add a String
argument as follow:假设电影的 Id 是
Movie.Id
并且是一个String
在您的Adapter class 中添加一个String
参数,如下所示:
var onItemClick:((String)->Unit)? = null
Invoke the lambdas in Adapter with the movieId in argument:使用参数中的 movieId 调用Adapter中的 lambda:
holder.itemView.setOnClickListener {
onItemClick?.invoke(realMovie.Id)
}
And then you can give this onItemClick
to your Adapter class然后你可以把这个
onItemClick
给你的适配器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:您可以通过意图在其他活动中获取您的movieId字符串:
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.如果
Id
是一个 int 适配 lambda 的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.