我想填充 ListView 以顯示特定值,該值將通過使用 Kotlin 的 Android Studio 中另一個類的資料作為標題。我知道如何填充 ListView,但我不確定如何獲取“title”值并將其放入 ListView 這是我希望它看起來的示例:

用于填充 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 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
}
}
}
簡單電影專案
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
}
}
uj5u.com熱心網友回復:
只需映射movies 陣列并映射它們的標題:
val movies = SimpleMovieSampleData.simpleMovieitemArray
val moviesTitles = movies.map { it.title }.toTypedArray()
val listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, moviesTitles)
movielist.adapter = listAdapter
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/388563.html
