0

Parent Model

data class Category(
    val category : String,
    val result : List<Result>? = null
    ) {
}

Child Model

@Parcelize
data class Result(
    val backdrop_path: String,
    val id: Int,
    val original_language: String,
    val original_title: String,
    val overview: String,
    val popularity: Double,
    val poster_path: String,
    val release_date: String,
    val title: String,
    val video: Boolean,
    val vote_average: Double,
    val vote_count: Int
) : Parcelable

Child Adapter

class MovieAdapter : ListAdapter<Result, MovieAdapter.MovieViewHolder> (DiffUtilCallBack()){
    inner class MovieViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
        val poster : ImageView = itemView.findViewById(R.id.ivMoviePoster)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
        return MovieViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_movie, parent, false))
    }

    override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
        val movie = getItem(position)
        val url = "https://image.tmdb.org/t/p/original" + movie.poster_path
        Glide.with(holder.itemView.context).load(url).into(holder.poster)
    }

    class DiffUtilCallBack : DiffUtil.ItemCallback<Result>(){
        override fun areItemsTheSame(oldItem: Result, newItem: Result): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Result, newItem: Result): Boolean {
            return newItem == oldItem
        }

    }
}

parent Adpter

 inner class  CategoryViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
        val movieCategory : TextView = itemView.findViewById(R.id.tvMovieCategory)
        val rvCategory : RecyclerView = itemView.findViewById(R.id.rvMovieCategory)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
        return CategoryViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_category_movie, parent, false))
    }

    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        val item = getItem(position)
        holder.movieCategory.text = item.category
        val movieAdapter = MovieAdapter()
        holder.apply {
            rvCategory.layoutManager = LinearLayoutManager(itemView.context, LinearLayoutManager.HORIZONTAL, false)
            rvCategory.adapter = movieAdapter
        }
        movieAdapter.submitList(item.result)
    }

    class DiffUtilCallBack : DiffUtil.ItemCallback<Category>(){
        override fun areItemsTheSame(oldItem: Category, newItem: Category): Boolean {
            return oldItem.category == newItem.category
        }

        override fun areContentsTheSame(oldItem: Category, newItem: Category): Boolean {
            return oldItem.category == newItem.category
        }
    }
}

Fragment

 val list = mutableListOf<List<Result>>()
        val category = listOf("upcoming", "popular")
        
        for(item in category){
            viewModel.movieList(item)
            viewModel.movieList.observe(viewLifecycleOwner, Observer {
                it.body()?.results?.let { it1 -> list.add(it1) }
            })
        }
        
        val mList = listOf(Category("Upcoming", list[0]), Category("Popular", list[1]))
        categoryAdapter.submitList(mList)

API Sample: https://api.themoviedb.org/3/movie/upcoming?api_key=2c2f00da593005799f2f1ecb3f773145&language=en-US&page=1

{
  "dates": {
    "maximum": "2023-09-07",
    "minimum": "2023-08-15"
  },
  "page": 1,
  "results": [
    {
      "adult": false,
      "backdrop_path": "/zN41DPmPhwmgJjHwezALdrdvD0h.jpg",
      "genre_ids": [
        28,
        878,
        27
      ],
      "id": 615656,
      "original_language": "en",
      "original_title": "Meg 2: The Trench",
      "overview": "An exploratory dive into the deepest depths of the ocean of a daring research team spirals into chaos when a malevolent mining operation threatens their mission and forces them into a high-stakes battle for survival.",
      "popularity": 1730.76,
      "poster_path": "/4m1Au3YkjqsxF8iwQy0fPYSxE0h.jpg",
      "release_date": "2023-08-02",
      "title": "Meg 2: The Trench",
      "video": false,
      "vote_average": 7,
      "vote_count": 446
    },
    {
      "adult": false,
      "backdrop_path": "/iEFuHjqrE059SmflBva1JzDJutE.jpg",
      "genre_ids": [
        16,
        10751,
        28,
        14,
        10749
      ],
      "id": 496450,
      "original_language": "fr",
      "original_title": "Miraculous - le film",
      "overview": "A life of an ordinary Parisian teenager Marinette goes superhuman when she becomes Ladybug. Bestowed with magical powers of creation, Ladybug must unite with her opposite, Cat Noir, to save Paris as a new villain unleashes chaos unto the city.",
      "popularity": 843.798,
      "poster_path": "/jazoSkOHR1H5X4ILIJBN1UfcEyA.jpg",
      "release_date": "2023-07-05",
      "title": "Miraculous: Ladybug & Cat Noir, The Movie",
      "video": false,
      "vote_average": 7.9,
      "vote_count": 459
    },
    {
      "adult": false,
      "backdrop_path": "/rLb2cwF3Pazuxaj0sRXQ037tGI1.jpg",
      "genre_ids": [
        18,
        36
      ],
      "id": 872585,
      "original_language": "en",
      "original_title": "Oppenheimer",
      "overview": "The story of J. Robert Oppenheimer’s role in the development of the atomic bomb during World War II.",
      "popularity": 810.697,
      "poster_path": "/8Gxv8gSFCU0XGDykEGv7zR1n2ua.jpg",
      "release_date": "2023-07-19",
      "title": "Oppenheimer",
      "video": false,
      "vote_average": 8.3,
      "vote_count": 1929
    },
    {
      "adult": false,
      "backdrop_path": "/lDCIQ1Qe7cRnhZ4ybQVVEbadMZ.jpg",
      "genre_ids": [
        27,
        53
      ],
      "id": 1008042,
      "original_language": "en",
      "original_title": "Talk to Me",
      "overview": "When a group of friends discover how to conjure spirits using an embalmed hand, they become hooked on the new thrill, until one of them goes too far and unleashes terrifying supernatural forces.",
      "popularity": 575.662,
      "poster_path": "/kw0Hegzpg08v37nRcmczCvqpl7d.jpg",
      "release_date": "2023-07-26",
      "title": "Talk to Me",
      "video": false,
      "vote_average": 7,
      "vote_count": 157
    },
    {
      "adult": false,
      "backdrop_path": "/waBWlJlMpyFb7STkFHfFvJKgwww.jpg",
      "genre_ids": [
        28,
        18
      ],
      "id": 678512,
      "original_language": "en",
      "original_title": "Sound of Freedom",
      "overview": "The story of Tim Ballard, a former US government agent, who quits his job in order to devote his life to rescuing children from global sex traffickers.",
      "popularity": 477.901,
      "poster_path": "/kSf9svfL2WrKeuK8W08xeR5lTn8.jpg",
      "release_date": "2023-07-03",
      "title": "Sound of Freedom",
      "video": false,
      "vote_average": 8.2,
      "vote_count": 361
    },
    {
      "adult": false,
      "backdrop_path": "/8FhKnPpql374qyyHAkZDld93IUw.jpg",
      "genre_ids": [
        9648,
        53,
        878
      ],
      "id": 536437,
      "original_language": "en",
      "original_title": "Hypnotic",
      "overview": "A detective becomes entangled in a mystery involving his missing daughter and a secret government program while investigating a string of reality-bending crimes.",
      "popularity": 354.435,
      "poster_path": "/3IhGkkalwXguTlceGSl8XUJZOVI.jpg",
      "release_date": "2023-05-11",
      "title": "Hypnotic",
      "video": false,
      "vote_average": 6.5,
      "vote_count": 395
    },
    {
      "adult": false,
      "backdrop_path": "/H6j5smdpRqP9a8UnhWp6zfl0SC.jpg",
      "genre_ids": [
        28,
        878
      ],
      "id": 565770,
      "original_language": "en",
      "original_title": "Blue Beetle",
      "overview": "Recent college grad Jaime Reyes returns home full of aspirations for his future, only to find that home is not quite as he left it. As he searches to find his purpose in the world, fate intervenes when Jaime unexpectedly finds himself in possession of an ancient relic of alien biotechnology: the Scarab.",
      "popularity": 293.033,
      "poster_path": "/vNfL4DYnonltukBrrgMmw94zMYL.jpg",
      "release_date": "2023-08-16",
      "title": "Blue Beetle",
      "video": false,
      "vote_average": 7.5,
      "vote_count": 21
    },
    {
      "adult": false,
      "backdrop_path": "/o9bbojtrrpl0yriiTmzC3Lp3OhA.jpg",
      "genre_ids": [
        28,
        10752
      ],
      "id": 840326,
      "original_language": "fi",
      "original_title": "Sisu",
      "overview": "Deep in the wilderness of Lapland, Aatami Korpi is searching for gold but after he stumbles upon Nazi patrol, a breathtaking and gold-hungry chase through the destroyed and mined Lapland wilderness begins.",
      "popularity": 288.236,
      "poster_path": "/ygO9lowFMXWymATCrhoQXd6gCEh.jpg",
      "release_date": "2023-01-27",
      "title": "Sisu",
      "video": false,
      "vote_average": 7.5,
      "vote_count": 1171
    },
    {
      "adult": false,
      "backdrop_path": "/qykUYxstHurdadXTF711AWZi0f8.jpg",
      "genre_ids": [
        16,
        10751,
        35
      ],
      "id": 916423,
      "original_language": "nl",
      "original_title": "Knor",
      "overview": "A young girl sets out to prove to her disapproving mother she can house-train the endearing but unruly little piglet she gets as a birthday gift from her estranged oddball grandfather.",
      "popularity": 239.632,
      "poster_path": "/6QbTLuLF3vslLn4K5IcIBas1Rh8.jpg",
      "release_date": "2022-07-01",
      "title": "Oink",
      "video": false,
      "vote_average": 7.8,
      "vote_count": 22
    },
    {
      "adult": false,
      "backdrop_path": "/w2nFc2Rsm93PDkvjY4LTn17ePO0.jpg",
      "genre_ids": [
        16,
        35,
        28
      ],
      "id": 614930,
      "original_language": "en",
      "original_title": "Teenage Mutant Ninja Turtles: Mutant Mayhem",
      "overview": "After years of being sheltered from the human world, the Turtle brothers set out to win the hearts of New Yorkers and be accepted as normal teenagers through heroic acts. Their new friend April O'Neil helps them take on a mysterious crime syndicate, but they soon get in over their heads when an army of mutants is unleashed upon them.",
      "popularity": 225.435,
      "poster_path": "/ueO9MYIOHO7M1PiMUeX74uf8fB9.jpg",
      "release_date": "2023-07-31",
      "title": "Teenage Mutant Ninja Turtles: Mutant Mayhem",
      "video": false,
      "vote_average": 7.4,
      "vote_count": 171
    },
    {
      "adult": false,
      "backdrop_path": "/nKOutYdpjpxdeftoXcDnSAaD2z8.jpg",
      "genre_ids": [
        53,
        27
      ],
      "id": 954388,
      "original_language": "en",
      "original_title": "Quicksand",
      "overview": "A married couple on the brink of divorce becomes trapped in quicksand while hiking through a Colombian rainforest. It’s a struggle for survival as they battle the elements of the jungle and must work together in order to escape.",
      "popularity": 225.156,
      "poster_path": "/cVLfO3CbVg8p5Qcaifq6AidOe2w.jpg",
      "release_date": "2023-08-31",
      "title": "Quicksand",
      "video": false,
      "vote_average": 0,
      "vote_count": 0
    },
    {
      "adult": false,
      "backdrop_path": "/jv4tiXAgaArMQo57jFMjvBEjmoa.jpg",
      "genre_ids": [
        28,
        18,
        12
      ],
      "id": 980489,
      "original_language": "en",
      "original_title": "Gran Turismo",
      "overview": "The ultimate wish fulfillment tale of a teenage Gran Turismo player whose gaming skills won him a series of Nissan competitions to become an actual professional racecar driver.",
      "popularity": 197.718,
      "poster_path": "/51tqzRtKMMZEYUpSYkrUE7v9ehm.jpg",
      "release_date": "2023-08-09",
      "title": "Gran Turismo",
      "video": false,
      "vote_average": 7.2,
      "vote_count": 77
    },
    {
      "adult": false,
      "backdrop_path": "/nUixrAu0tLn9PwfN7iaBoSAiHkm.jpg",
      "genre_ids": [
        27,
        14
      ],
      "id": 635910,
      "original_language": "en",
      "original_title": "The Last Voyage of the Demeter",
      "overview": "The crew of the merchant ship Demeter attempts to survive the ocean voyage from Carpathia to London as they are stalked each night by a merciless presence onboard the ship.",
      "popularity": 187.129,
      "poster_path": "/nrtbv6Cew7qC7k9GsYSf5uSmuKh.jpg",
      "release_date": "2023-08-09",
      "title": "The Last Voyage of the Demeter",
      "video": false,
      "vote_average": 7.8,
      "vote_count": 34
    },
    {
      "adult": false,
      "backdrop_path": "/zjmHE7pDSOFVOsfDTwSsczwXPdP.jpg",
      "genre_ids": [
        16,
        35,
        18
      ],
      "id": 783675,
      "original_language": "ja",
      "original_title": "THE FIRST SLAM DUNK",
      "overview": "Shohoku's “speedster” and point guard, Ryota Miyagi, always plays with brains and lightning speed, running circles around his opponents while feigning composure. In his second year of high school, Ryota plays with the Shohoku High School basketball team along with Sakuragi, Rukawa, Akagi, and Mitsui as they take the stage at the Inter-High School National Championship. And now, they are on the brink of challenging the reigning champions, Sannoh Kogyo High School.",
      "popularity": 176.172,
      "poster_path": "/995t1sb4ummXHBfKlXLSM1IAEjc.jpg",
      "release_date": "2022-12-03",
      "title": "The First Slam Dunk",
      "video": false,
      "vote_average": 7.8,
      "vote_count": 167
    },
    {
      "adult": false,
      "backdrop_path": "/nYDPmxvl0if5vHBBp7pDYGkTFc7.jpg",
      "genre_ids": [
        27
      ],
      "id": 709631,
      "original_language": "en",
      "original_title": "Cobweb",
      "overview": "Eight year old Peter is plagued by a mysterious, constant tapping from inside his bedroom wall—one that his parents insist is all in his imagination. As Peter's fear intensifies, he believes that his parents could be hiding a terrible, dangerous secret and questions their trust.",
      "popularity": 175.948,
      "poster_path": "/cGXFosYUHYjjdKrOmA0bbjvzhKz.jpg",
      "release_date": "2023-07-19",
      "title": "Cobweb",
      "video": false,
      "vote_average": 6.8,
      "vote_count": 120
    },
    {
      "adult": false,
      "backdrop_path": "/c6Splshb8lb2Q9OvUfhpqXl7uP0.jpg",
      "genre_ids": [
        28,
        53
      ],
      "id": 717930,
      "original_language": "en",
      "original_title": "Kandahar",
      "overview": "After his mission is exposed, an undercover CIA operative stuck deep in hostile territory in Afghanistan must fight his way out, alongside his Afghan translator, to an extraction point in Kandahar, all whilst avoiding elite enemy forces and foreign spies tasked with hunting them down.",
      "popularity": 174.677,
      "poster_path": "/lCanGgsqF4xD2WA5NF8PWeT3IXd.jpg",
      "release_date": "2023-05-25",
      "title": "Kandahar",
      "video": false,
      "vote_average": 6.6,
      "vote_count": 333
    },
    {
      "adult": false,
      "backdrop_path": "/xkXsV1WOiKfAJ6dzXiavdwsZ3E2.jpg",
      "genre_ids": [
        28,
        53,
        27
      ],
      "id": 799379,
      "original_language": "ko",
      "original_title": "늑대사냥",
      "overview": "While under heavily armed guard, the dangerous convicts aboard a cargo ship unite in a coordinated escape attempt that soon escalates into a bloody, all-out riot. But as the fugitives continue their brutal campaign of terror, they soon discover that not even the most vicious among them is safe from the horror they unknowingly unleashed from the darkness below deck.",
      "popularity": 144.173,
      "poster_path": "/dniWicB6fa7NvpGbguxWlNPMc5f.jpg",
      "release_date": "2022-09-21",
      "title": "Project Wolf Hunting",
      "video": false,
      "vote_average": 6.4,
      "vote_count": 224
    },
    {
      "adult": false,
      "backdrop_path": "/qElNES0sHVQcbzvGrTx7ccpGzij.jpg",
      "genre_ids": [
        878,
        28,
        18
      ],
      "id": 842675,
      "original_language": "zh",
      "original_title": "流浪地球2",
      "overview": "Humans built huge engines on the surface of the earth to find a new home. But the road to the universe is perilous. In order to save earth, young people once again have to step forward to start a race against time for life and death.",
      "popularity": 143.066,
      "poster_path": "/pR858ihc6Ls9xohpdRJVjV787ml.jpg",
      "release_date": "2023-01-22",
      "title": "The Wandering Earth II",
      "video": false,
      "vote_average": 7.2,
      "vote_count": 316
    },
    {
      "adult": false,
      "backdrop_path": "/Als2QyqbLgT5G3rwTGJta8QQfqP.jpg",
      "genre_ids": [
        35,
        14
      ],
      "id": 616747,
      "original_language": "en",
      "original_title": "Haunted Mansion",
      "overview": "A woman and her son enlist a motley crew of so-called spiritual experts to help rid their home of supernatural squatters.",
      "popularity": 131.795,
      "poster_path": "/kpLx1gDYolWLBNOmuFVvrqFKQK3.jpg",
      "release_date": "2023-07-26",
      "title": "Haunted Mansion",
      "video": false,
      "vote_average": 7,
      "vote_count": 141
    },
    {
      "adult": false,
      "backdrop_path": "/8iUYdAdkfiIQFMrg2Kxe4gVbXdN.jpg",
      "genre_ids": [
        35
      ],
      "id": 912908,
      "original_language": "en",
      "original_title": "Strays",
      "overview": "When Reggie is abandoned on the mean city streets by his lowlife owner, Doug, Reggie is certain that his beloved owner would never leave him on purpose. But once Reggie falls in with a fast-talking, foul-mouthed stray who loves his freedom and believes that owners are for suckers, Reggie finally realizes he was in a toxic relationship and begins to see Doug for the heartless sleazeball that he is.",
      "popularity": 125.577,
      "poster_path": "/wDyIj84k4zpEMbhNbX4zaUW0Jkj.jpg",
      "release_date": "2023-08-17",
      "title": "Strays",
      "video": false,
      "vote_average": 6.2,
      "vote_count": 6
    }
  ],
  "total_pages": 28,
  "total_results": 544
}

The child adapter accepts List<Result>. And the parent adapter accepts List<Category>. The data is coming from API and observing the live data. What I want to do that for every category item a response is generated, and I want to pass to the parent adapter but I am unable to store the result which is coming from the response. I am new to this so I am not getting what is going on here.

Harsh Panchal
  • 146
  • 1
  • 1
  • 6

0 Answers0