android - How can I convert a list to string
问题描述
data class Movie(
@SerializedName("movie_name")
val movieName : String
@SerializedName("genres")
val movieGenre : List<String>){}
How can I convert a list to string created every object in this data class
解决方案
You can use additional property in Movie
class to convert list to string, e.g. val genre
:
data class Movie(
@SerializedName("movie_name")
val movieName: String,
@SerializedName("genres")
val movieGenre: List<String>
) {
val genre: String
get() = buildString { // builds new string using append() method on each item of movieGenre
movieGenre.forEach { append(it) }
}
}
Here is how to access it:
val movie: Movie = getMoviewFromInternet()
val movieGenre: String = movie.genre
推荐阅读
- python - 在另一个脚本中使用类中的变量
- javascript - 使用 react.js 在 Visual Studio 中键入“npm start”时出错
- python - 使用 Parquet 文件处理 Arrow 中的 UUID 值
- angular - 无法在 Html 中显示来自 API 的图像(使用角度 7)
- vb.net - 旋转平面(以 2D 方式投影和绘制)在某些边缘看起来是弯曲的而不是笔直的
- css - 在后端删除付款详细信息 Woocommerce 订单页面
- servlets - 如何从 servlet 读取以下路径?
- node.js - 节点 JS Plesk 设置
- python - 如何获取 api 中字典的特定信息
- c# - Type.GetConstructor 返回 null 并且 Type.GetConstructors 为空