如何将对象名称添加到列表中,以便通过适配器进一步显示。这是我通过 Retrofit 2 获得的 API
接下来,我提出这样的请求:
测试英雄.kt
data class TestHero (@SerializedName("global") val global: PlayerInf,
@SerializedName("legends")val legends: AllLegends)
data class PlayerInf (val name: String, val uid: Long, val avatar: String, val platform: String,
val level: Int, val toNextLevelPercent: Int, val internalUpdateCount: Int, val bans: BanInf, val rank: RankInf)
data class BanInf (val isActive: Boolean, val remainingSeconds: Int)
data class RankInf (val rankScore: Int, val rankName: String, val rankDiv: Int, val rankImg: String)
data class AllLegends (val all: Revenant)
data class Revenant (val ImgAssets: String)
data class Horizon (val data : ArrayList<Rang>, val ImgAssets: String)
data class Rang (val t0 : String)
也就是说,我需要创建每个对象Revenant, Horizon, Crypto
等。但我不明白如何在 textView 的适配器中显示这些名称
HeroesAdapter.kt
class HeroesAdapter(context: Context, heroes: List<TestHero>): BaseAdapter() {
private val context = context
private val heroes = heroes
override fun getCount(): Int {
return heroes.count()
}
override fun getItem(position: Int): Any {
return heroes[position]
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
// categoryView = LayoutInflater.from(context).inflate(R.layout.activity_heroes, null)
val listheroView = LayoutInflater.from(context).inflate(R.layout.list_hero_view, parent, false)
// val categoryImage: ImageView = categoryView.findViewById(R.id.heroesImageView)
val heroText: TextView = listheroView.findViewById(R.id.textHeroView)
val category = heroes[position]
heroText.text = category.legends.all.toString()
return listheroView
}
}
在哪里,点击后,我已经从数据中接收数据
"legends": {
"selected": {
"LegendName": "Octane",
"data": [
{
"name": "Special event kills",
"value": 815,
"key": "specialEvent_kills"
},
{
"name": "Special event wins",
"value": 37,
"key": "specialEvent_wins"
},
{
"name": "Special event damage",
"value": 321873,
"key": "specialEvent_damage"
}
],
"gameInfo": {
"skin": "Arachnoid Rush",
"skinRarity": "Legendary",
"frame": "Shark Teeth",
"frameRarity": "Legendary",
"pose": "All. Day.",
"poseRarity": "Rare",
"intro": "Death catches up to everyone",
"introRarity": "Rare",
"badges": [
{
"name": "Venomous",
"value": 0,
"category": "Octane"
},
{
"name": "Wild Frontier Level: Season 1",
"value": 112,
"category": "Account Badges"
},
{
"name": "Fortune's Favor Level: Season 5",
"value": 113,
"category": "Account Badges"
}
]
},
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/octane.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/octane.jpg"
}
},
"all": {
"Revenant": {
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/revenant.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/revenant.jpg"
}
},
"Crypto": {
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/crypto.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/crypto.jpg"
}
},
"Horizon": {
"data": [
{
"name": "Season 7 wins",
"value": 1,
"key": "wins_season_7",
"rank": {
"rankPos": 59006,
"topPercent": 69.87
},
"rankPlatformSpecific": {
"rankPos": 53782,
"topPercent": 69.14
}
},
{
"name": "Special event kills",
"value": 101,
"key": "specialEvent_kills",
"rank": {
"rankPos": 77442,
"topPercent": 64.21
},
"rankPlatformSpecific": {
"rankPos": 52573,
"topPercent": 60.51
}
},
{
"name": "Special event damage",
"value": 47004,
"key": "specialEvent_damage",
"rank": {
"rankPos": 74182,
"topPercent": 57.92
},
"rankPlatformSpecific": {
"rankPos": 51199,
"topPercent": 54.54
}
}
],
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/horizon.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/horizon.jpg"
}
},
为了实现我的解决方案,感谢@woesss 的提示,我将数据解析为
Map
然后我在RecyclerView中全部展示了,你可以看这里的实现