RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1319937
Accepted
Ethernets
Ethernets
Asked:2022-08-21 16:10:54 +0000 UTC2022-08-21 16:10:54 +0000 UTC 2022-08-21 16:10:54 +0000 UTC

RecyclerView中无法显示所有数组数据

  • 772

有这样的适配器吗?

class HeroesRecyclerAdapter(val context: Context, val heroes: List<TestHero>): RecyclerView.Adapter<HeroesRecyclerAdapter.Holder>() {

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val heroesName = itemView?.findViewById<TextView>(R.id.textHeroView)

        fun bindHeroes (hero: TestHero, context: Context){
           //heroesName?.text = hero.legends.all.keys.first()
            for((key) in hero.legends.all){
                Log.d("HeroesActivity", key)
                //holder.heroesName?.text = key
                heroesName?.text = key

            }

        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val view = LayoutInflater.from(context).inflate(R.layout.list_hero_view, parent, false)
        return  Holder(view)
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder?.bindHeroes(heroes[position], context)
    }

    override fun getItemCount(): Int {
        return heroes.count()
    }
}

当循环填充TextView时,数据被覆盖,只显示数组的最后一个值,如何正确显示数组中一列中的所有数据,以便进一步从列表中选择一个类别?

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 (@SerializedName("all") val all: Map<String, LegendWrapper> = emptyMap())


data class LegendWrapper(
    val data: List<PlayerPerformance>? = emptyList()

)
data class PlayerPerformance(val name: String, val value: Int, val key: String)

如果在不使用数组的情况下更改代码,那么我将获得 1 个字段中的所有键

class TestRecyclerAdapter(val cont: Context): RecyclerView.Adapter<TestRecyclerAdapter.Holders>() {
    var heroes: List<AllLegends> = emptyList()

    inner class Holders(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val heroesName = itemView?.findViewById<TextView>(R.id.textHeroView)

        fun bindHeroes (hero: AllLegends, context: Context){
            heroesName?.text = hero.all.keys.toString()


        }
}

在此处输入图像描述

android
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Ethernets
    2022-08-21T18:35:54Z2022-08-21T18:35:54Z

    我是通过下面的方式解决的,不知道对不对,想听听更多有经验的人的意见,下面我给出整个代码:适配器本身

    class HeroesRecyclerAdapter(val context: Context, var heroes: ArrayList<String>): RecyclerView.Adapter<HeroesRecyclerAdapter.Holder>() {
    
    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val heroesName = itemView?.findViewById<TextView>(R.id.textHeroView)
    
        fun bindHeroes (heroName: String, context: Context){
    
    
            heroesName?.text = heroName
           //heroesName?.text = hero.legends.all.keys.first()
           // for((key) in hero.legends.all){
          //      Log.d("HeroesActivity", key)
                //holder.heroesName?.text = key
         //       heroesName?.text = key
    
         //   }
    
        }
    
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val view = LayoutInflater.from(context).inflate(R.layout.list_hero_view, parent, false)
        return  Holder(view)
    }
    
    override fun onBindViewHolder(holder: Holder, position: Int) {
       holder?.bindHeroes(heroes[position], context)
    
    }
    
    override fun getItemCount(): Int {
        return heroes.count()
     }
    }
    

    英雄活动

    class HeroesActivity : AppCompatActivity() {
    
        lateinit var heroesAdapt : HeroesRecyclerAdapter
        lateinit var testHeroesAdapt : TestRecyclerAdapter
    
        val listHero = ArrayList<TestHero>()
        val recList = ArrayList<String>()
        private val TAG = "HeroesActivity"
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_heroes)
    
            getCurrentData()
    
            heroesAdapt = HeroesRecyclerAdapter(this, recList)
            heroesListView.adapter = heroesAdapt
    
            val layoutManager = LinearLayoutManager(this)
            heroesListView.layoutManager = layoutManager
    
    
            /*
            testHeroesAdapt = TestRecyclerAdapter(this)
            heroesListView.adapter = testHeroesAdapt
    
            val layoutManager = LinearLayoutManager(this)
            heroesListView.layoutManager = layoutManager
          */
    
        }
    
        private fun getCurrentData() {
            val api = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ApiRequest::class.java)
    
            GlobalScope.launch(Dispatchers.IO) {
                val response = api.herList().awaitResponse()
                if (response.isSuccessful) {
                    val data = response.body()!!
                    Log.d(TAG, data.toString())
    
                    withContext(Dispatchers.Main){
    
                        listHero.add(data)
    
                        heroesAdapt.apply {
    
                            for((key) in data.legends.all){
                                Log.d("HeroesActivity", key)
                                //holder.heroesName?.text = key
                                recList.add(key)
                            }
    
                            notifyDataSetChanged()
                        }
    
    
    
                        textNickname.text = "${data.global.name} (${data.global.rank.rankDiv} divisions)"
                        textLvl.text = "Level: ${data.global.level.toString()}"
                        when(data.global.rank.rankName){
                            "Silver" -> textNickname.setTextColor(Color.parseColor("#7A7A79"))
                            "Gold" ->   textNickname.setTextColor(Color.parseColor("#E6D600"))
                            "Platinum" -> textNickname.setTextColor(Color.parseColor("#36BBCE"))
                        }
    
                    }
    
                }
            }
        }
    }
    

    我不知道这段代码的实现有多正确,但我会附上截图的结果。

     heroesAdapt.apply {
    
                            for((key) in data.legends.all){
                                Log.d("HeroesActivity", key)
                                //holder.heroesName?.text = key
                                recList.add(key)
                            }
    
                            notifyDataSetChanged()
                        }
    

    知道如何做得更好的人,请写信,我不想在错误的情况下立即学习语言。谢谢

    在此处输入图像描述

    • 1
  2. Dewerro
    2022-08-21T17:02:41Z2022-08-21T17:02:41Z

    试试这样:

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            when (holder) {
                is Holder -> {
                    holder.bindHeroes(heroes[position])
                }
            }
        }
    
    • 0

相关问题

  • 来自片段的列表落后于 BottomNavigationView

  • 无法将变量从 Activity 传递到 Fragment

  • 构建与完成的片段略有不同的片段的最佳方法是什么?

  • 如何更改来自服务器的响应中的日期格式?

  • 谷歌地图在应用程序的发布版本中不起作用

  • 材料设计按钮。单击按钮上的可选区域!

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5