简体   繁体   English

[KOTLIN]我正在尝试将数据从我的回收站视图传递到另一个活动,但不知道如何

[英][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.

相关问题 如何将数据从回收站视图传递到另一个活动 - How to pass data from recycler view to another activity 如何将 Recycler 视图中的详细信息传递给另一个活动? - How to pass the details from Recycler view to another activity? 如何将json图像从回收者视图传递到另一个活动 - How to pass json image from recycler view to another activity 如何将数据从活动传递到 android 中的回收器视图适配器 - How to pass data from a activity to a recycler view adapter in android 我正在尝试从嵌套回收器视图中打开自定义 chrome 选项卡,但无法从 Activity 外部调用 startActivity() 时出错 - I am trying to open custom chrome tab from nested recycler view but couldn't having error Calling startActivity() from outside of an Activity 如何将Textview数据从回收者视图带到另一个活动? - How to take textview data from recycler view to another activity? 当我使用我想传递餐厅 ID 的意图将数据从回收器适配器传递到活动时,我的应用程序崩溃了 - my app is crashing when i am passing data from recycler adapter to activity using intents i want to pass restaurant id 如果我不知道活动2何时结束,如何将数据从活动2返回到活动1? - How to return data from activity2 to activity1 if I don't know when activity2 will end? 我正在尝试使用回收站视图从 Firebase 数据库中检索数据,但出现错误 - i am trying to retrieve the data from fire base database using recycler view but i am getting errors in it Kotlin(anko)使用回收者视图将主要活动中的数据解析为另一个活动[CLOSE] - kotlin(anko) parsing data in main activity to another activity with recycler view[CLOSE]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM