我正在尝试将Movieid从当前回收程序视图获取到另一个Activity我从另一个API获取了数据,但要显示有关电影的详细信息,我需要Movieid,但无法传递数据
这是我的主课
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) {
}
}
这是我的适配器类
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)
}
}
这是我想要数据的类
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
}
}
我是编程新手
1条答案
按热度按时间elcex8rz1#
您的
OnItemClick
lambda错误假设电影的ID为
Movie.Id
,并且是String
。在Adapter类中添加String
参数,如下所示:使用参数中的movieId调用Adapter中的lambdas:
然后,您可以将此
onItemClick
提供给Adapter类您可以通过Intent在其他Activity中获取您的movieIdString:
如果
Id
是一个int,则修改lambda的参数。