I have an arrayList<> of strings and I added 10 strings to it.
private val names: ArrayList<String> = arrayListOf()
These are the strings added
[G.I. Joe: The Rise of Cobra, Creed 2, The Equalizer 2, Ride Along 2, Mission Impossible, Mission Impossible II, Mission Impossible III, Mission Impossible: Ghost Protocol, Mission Impossible: Fallout, Suicide Squad]
I have a recycler view with its adapter as follows:
class MovieSeriesAdapter(
private val movie: MoviesInSeries,
private val movieNameList: ArrayList<String>?,
private val restMoviesPosition: Int,
): RecyclerView.Adapter<MovieSeriesAdapter.ViewHolder>() {
class ViewHolder(binding: SeriesMoviesItemBinding): RecyclerView.ViewHolder(binding.root) {
val mainThing = binding.mainThing
val tvMovieNameAndYear = binding.seriesMovieNameAndYear
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(SeriesMoviesItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (position % 2 != 0) {
holder.mainThing.setBackgroundColor(Color.parseColor("#E8E8E8"))
}
val RestMoviesList = Constants.getRestOfSeriesMovies()
var targetPosition: Int = 0
when (movie.originalMovieName) {
"G.I. Joe: Retaliation" -> targetPosition = 0
"Creed" -> targetPosition = 1
"The Equalizer" -> targetPosition = 2
"Ride Along" -> targetPosition = 3
"Mission Impossible" -> targetPosition = 4
"Suicide Squad" -> targetPosition = 9
"Venom" -> targetPosition = 10
}
val targetMovieName = movieNameList!![targetPosition]
holder.tvMovieNameAndYear.text = "$targetMovieName ($targetMovieDate)"
}
override fun getItemCount(): Int {
return 5
}
The thing is that sometimes I need the recycler view to show 5 items, as in the case below. For Example, I have 5 movies of mission impossible and I want to show them. But the targetPosition
is an integer variable of only one number.
I tried creating an array list of only the mission impossible movies but it showed identical 5 items with all of the data in one item.
How do I make it that when I need 5 items to be displayed, each item should get a diffent value from the name array list.
CodePudding user response:
I'll recommend you to directly use the value of position for targetValue, inside onBindViewHolder while setting the value of text.
CodePudding user response:
You should just give the adapter a list of the 5 you want to show.
Also, a lot of the code you wrote doesn't make a lot of sense.
For example
when (movie.originalMovieName) {
You decide here something based on movie.originalMovieName, but this movie
is passed in the constructor of the adapter, which already doesn't make sense, but it also means that it will be the same movie for every item.