I want to populate the ListView of to show a specific value, which is going to be title through the data of another class in Android Studio using Kotlin. I know how to populate the ListView, but I am not sure on how to get the "title" value and put it into the ListView This is an example of what I want it to look:
Class used to populate the ListView:
class SimpleViewListOfMoviesActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_view_list_of_movies)
val movies = SimpleMovieItem()
val test = arrayOf(movies.title)
val listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, test)
movielist.adapter = listAdapter
}
}
Class with the data inside:
class SimpleMovieSampleData {
companion object{
var simpleMovieitemArray : ArrayList<SimpleMovieItem>
init {
simpleMovieitemArray = ArrayList<SimpleMovieItem>()
populateSimpleMovieItem()
}
fun populateSimpleMovieItem() : ArrayList<SimpleMovieItem>{
simpleMovieitemArray.add(
SimpleMovieItem("Super-assassin John Wick returns with a \$14 million price tag on his head and an army of bounty-hunting killers on his trail. After killing a member of the shadowy international assassin’s guild, the High Table, John Wick is excommunicado, but the world’s most ruthless hit men and women await his every turn.",
"August 23, 2019",
"English",
"John Wick: Chapter 3 - Parabellum ")
)
simpleMovieitemArray.add(
SimpleMovieItem("After faking his death, a tech billionaire recruits a team of international operatives for a bold and bloody mission to take down a brutal dictator.",
"December 13, 2019",
"English",
"6 Underground ")
)
simpleMovieitemArray.add(
SimpleMovieItem("After fighting his demons for decades, John Rambo now lives in peace on his family ranch in Arizona, but his rest is interrupted when Gabriela, the granddaughter of his housekeeper María, disappears after crossing the border into Mexico to meet her biological father. Rambo, who has become a true father figure for Gabriela over the years, undertakes a desperate and dangerous journey to find her.",
"December 17, 2019",
"English",
"Rambo: Last Blood ")
)
return simpleMovieitemArray
}
}
}
SimpleMovieItem
package com.nyp.sit.movieviewerbasic_starter
class SimpleMovieItem(
var overview: String? = null,
var release_date: String? = null, var original_langauge: String? = null,
var title: String? = null
) {
init {
this.overview = overview
this.release_date = release_date
this.original_langauge = original_langauge
this.title = title
}
}
CodePudding user response:
Just map through the movies
array and map their titles:
val movies = SimpleMovieSampleData.simpleMovieitemArray
val moviesTitles = arrayOf(movies.map { it.title })
val listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, moviesTitles)
movielist.adapter = listAdapter