【问题标题】:Populate ListView with values from another class用另一个类的值填充 ListView
【发布时间】:2022-01-22 16:07:18
【问题描述】:

我想填充 ListView 以显示特定值,该值将通过 Android Studio 中使用 Kotlin 的另一个类的数据作为标题。我知道如何填充 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
        }

    }
}

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

    }


}

【问题讨论】:

  • 请尝试详细说明您想要实现的目标

标签: android android-studio kotlin


【解决方案1】:

只需映射 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

如果您需要显示带有点击电影标题的 toast 消息,您可以使用 setOnItemClickListener 方法:

movielist.setOnItemClickListener { parent, view, position, id -> 
    val movieTitle = listAdapter.getItemAtPosition(position)
    Toast.makeText(this@ SimpleViewListOfMoviesActivity, movieTitle, Toast.LENGTH_SHORT).show()
}

【讨论】:

  • 这正是我所需要的,但我想要的是每部电影的标题都溢出到每一行,因为我想尝试使用 Toast 来显示每部电影的标题,如上图所示。删除“arrayOf”应该可以解决这个问题吗?
  • 尝试使用toTypedArray()函数将列表转换为数组。如果要使用 Toast,则需要将列表转换为字符串。你想显示什么信息?
  • 我只是想在单击它时显示电影的标题作为测试。
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 2012-07-26
  • 2014-08-05
  • 1970-01-01
  • 2014-06-17
  • 2015-10-08
  • 2022-08-22
  • 1970-01-01
相关资源
最近更新 更多