首页 > 解决方案 > 更改适配器中未指定数量的对象的适配器中外部对象的属性

问题描述

我是 Kotlin 的新手,我想尽可能高效地开始编程,这意味着在此示例中,我的适配器中有未指定数量的对象。我正在尝试将按钮列表 2 设置为未指定,然后执行 onClick 事件,这应该更改按钮列表的 1 文本,但我正在努力使其在“MyCustomAdapter:BaseAdapter() ”。也许在 Adapter 中指定它?我不太确定,一些帮助会很好,谢谢。

class ChoosingSpells : AppCompatActivity() {

private fun spellSpec(spellCode: Int, index: Int): String {                                        // going to be server function...or partly made from server
    val returnSpec = when(spellCode) {
        3 -> arrayOf("Fire Ball","@drawable/firespell", "20","level","description")
        4 -> arrayOf("Freezing touch", "@drawable/icespell","30","level","description")
        5 -> arrayOf("Wind hug", "@drawable/windspell","40","level","description")
        else -> arrayOf("null","null","null")
    }
    return returnSpec[index]
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_choosing_spells)

    val listView = findViewById<ListView>(R.id.choosing_listview)

    textLabel.text  = "This works in here"
    listView.adapter = MyCustomAdapter()
}

    private class MyCustomAdapter: BaseAdapter() {


        private val learnedSpells = arrayOf(3,4,5,0,0,3,4,5,0,0,3,4,5,0,0,3,4,5,0,0,3,4,5,0,0,3,4,5,0,0,3,4,5,0,0,3,4,5,0,0)
        private val spellsInUse = arrayOf(0,0,0,0,0)


        override fun getCount(): Int {
            return (learnedSpells.size/2)
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getItem(position: Int): Any {
            return "TEST STRING"
        }

        override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
            val rowMain: View

            if (convertView == null) {
                val layoutInflater = LayoutInflater.from(viewGroup!!.context)
                rowMain = layoutInflater.inflate(R.layout.row_choosingspells, viewGroup, false)
                val viewHolder = ViewHolder(rowMain.button1, rowMain.button2)
                rowMain.tag = viewHolder
            } else {
                rowMain = convertView
            }
            val viewHolder = rowMain.tag as ViewHolder

            viewHolder.button1.setBackgroundResource(getDrawable(learnedSpells[if(position==0){0}else{position*2}]))
            viewHolder.button2.setBackgroundResource(getDrawable(learnedSpells[if(position==0){1}else{position*2+1}]))
            var clicks1 = 0
            var clicks2 = 0
            viewHolder.button1.setOnClickListener {
                clicks1++
                if(clicks1==1){         //single click
                    /*textLabel.text = spellSpec(learnedSpells[if(position==0){0}else{position*2}],0)  I just want to change text of an external label, which is not part of the adapter here
                    textLevel.text = spellSpec(learnedSpells[if(position==0){0}else{position*2}],3)
                    textStats.text = spellSpec(learnedSpells[if(position==0){0}else{position*2}],2)
                    textDescription.text = spellSpec(learnedSpells[if(position==0){0}else{position*2}],4)*/
                    textLabel.text  = "This doesn't work here"
                }else if(clicks1==2){

                }
                clicks1=0
            }
            viewHolder.button2.setOnClickListener {
                clicks2++
                if(clicks2==1){         //single click
                    /*textLabel.text = spellSpec(learnedSpells[if(position==0){1}else{position*2+1}],0)
                    textLevel.text = spellSpec(learnedSpells[if(position==0){1}else{position*2+1}],3)
                    textStats.text = spellSpec(learnedSpells[if(position==0){1}else{position*2+1}],2)
                    textDescription.text = spellSpec(learnedSpells[if(position==0){1}else{position*2+1}],4)*/
                }else if(clicks2==2){   //double click

                }
                clicks2=0
            }
            return rowMain
        }
        private fun getDrawable(index:Int): Int {
            return(when(index) {
                3 -> R.drawable.firespell
                4 -> R.drawable.icespell
                5 -> R.drawable.windspell
                0 -> R.drawable.shield
                else -> NULL
            }
            )
        }

        private class ViewHolder(val button1: TextView, val button2: TextView)

    }

}

标签: androidkotlinandroid-adapter

解决方案


刚刚添加了一个新的构造函数来传递所需的数据。


推荐阅读