【发布时间】:2022-01-23 07:45:53
【问题描述】:
我有这个使用 Kotlin 的 Android 应用程序,它填充 ListView 以显示电影标题。我目前面临的问题是我不确定如何 Intent 与 ListView 标题相关的所有数据。 一个例子是这样的,如果我点击“Jumanji”标题,应用程序会启动新活动并显示与点击的电影标题相关的所有信息
到目前为止,我只能填充 ListView 和 Intent,只有点击的电影的标题,但不知道如何做其余的
我填充电影列表的活动:
class SimpleViewListOfMoviesActivity : AppCompatActivity() {
val INTENT_CODE = 1;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_view_list_of_movies)
val movies = simpleMovieitemArray
val movie_tiles = movies.map {it.title}
// val movie_overviews = movies.map {it.overview} (testing ignore)
val listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, movie_tiles)
movielist.adapter = listAdapter
movielist.onItemClickListener = object : AdapterView.OnItemClickListener {
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
{
// displayToast("You have selected " + parent?.adapter?.getItem(position))
MovieIntent(parent?.adapter?.getItem(position) as String)
}
}
}
fun displayToast(message:String){
Toast.makeText(this,message, Toast.LENGTH_LONG).show()
}
fun MovieIntent(message:String)
{
var myIntent = Intent(this, SimpleItemDetailActivity::class.java)
myIntent.putExtra("movieTitle", message)
startActivityForResult(myIntent,INTENT_CODE)
}
}
我通过 Intent 获取数据并显示的活动
class SimpleItemDetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.simple_activity_item_detail)
var movieTitleFromList = intent.getStringExtra("movieTitle")
movie_title.text = "$movieTitleFromList"
}
数据从何而来”
class SimpleMovieSampleData {
companion object{
var simpleMovieitemArray : ArrayList<SimpleMovieItem>
init {
simpleMovieitemArray = ArrayList<SimpleMovieItem>()
populateSimpleMovieItem()
}
fun populateSimpleMovieItem() : ArrayList<SimpleMovieItem>{
simpleMovieitemArray.add(
SimpleMovieItem("Elsa, Anna, Kristoff and Olaf head far into the forest to learn the truth about an ancient mystery of their kingdom.",
"November 22, 2019",
"English",
"Frozen II (2019)")
)
simpleMovieitemArray.add(
SimpleMovieItem("In Jumanji: The Next Level, the gang is back but the game has changed. As they return to rescue one of their own, the players will have to brave parts unknown from arid deserts to snowy mountains, to escape the world's most dangerous game.",
"December 13, 2019",
"English",
"Jumanji: The Next Level")
)
//two examples of the movies
return simpleMovieitemArray
}
}
}
【问题讨论】:
-
你应该给你的电影一个
id,并通过intent传递id。然后在SimpleItemDetailActivity中根据id过滤您的电影列表
标签: android android-studio kotlin listview