更改适配器中外部对象的属性,其中适配器中未指定对象数

时间:2018-09-13 06:30:13

标签: android kotlin android-adapter

我在Kotlin中还很陌生,我想开始尽可能高效地进行编程,这意味着在本示例中,适配器中的对象数未指定。我试图将按钮2的列表设为未指定,然后执行onClick事件,该事件应更改按钮列表的1个文本ot,但是我正在努力使其在“ MyCustomAdapter:BaseAdapter()”中可见并可编辑”。 也许在适配器内部指定它?我不太确定,谢谢您的帮助。

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)

    }

}

1 个答案:

答案 0 :(得分:0)

只需添加一个新的构造函数即可传递所需的数据。