如何刷新/更新listView适配器中的数据

时间:2018-09-21 18:52:29

标签: android kotlin custom-adapter

所以我试图在2个ListView之间建立连接,在第一个ListView中单击一个项目后,它会更改一个Array(取决于我喜欢的内容),该Array会作为构造函数进入第二个ListView,根据数组中的内容更改背景,这意味着:我在1. ListView,2. ListView中单击按钮(此示例更改背景)。我找到了一个叫做“ notifyDataSetChanged”的东西,但是这对我没有任何帮助,也试图在第二个ListView中创建一个更新方法,但都没有解决。感谢任何帮助,谢谢

class ChoosingSpells : AppCompatActivity() {
    private var learnedSpells = listOf(3,4,0,0,5,5)
    private val chosenSpells = arrayOf(5,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
    private var clickedSpell = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_choosing_spells)
        buttonChoose.visibility = View.INVISIBLE

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

        choosing_listview.adapter = LearnedSpellsView(textLabel,textLevel,textDescription,textStats, learnedSpells, clickedSpell, buttonChoose)
        chosen_listView.adapter = ChosenSpellsView(chosenSpells)

        buttonChoose.setOnClickListener{
            for (i in 1..19) {
                if(chosenSpells[i] != 0){
                    chosenSpells[i] = clickedSpell
                    buttonChoose.text = chosenSpells[i].toString()
                    break
                }
            }
        }
    }

    private class LearnedSpellsView(var textViewLabel: TextView, var textViewLevel: TextView, var textViewDescription: TextView, var textViewStats: TextView, val learnedSpells: List<Int>, var clickedSpell:Int, var buttonChoose: Button) : BaseAdapter() {

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

        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
            try{
                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}]))

                if(learnedSpells[if(position==0){0}else{position*2}]!=0){
                    viewHolder.button1.setOnClickListener {
                        textViewLabel.text = spellSpec(learnedSpells[if(position==0){0}else{position*2}],0)
                        textViewLevel.text = spellSpec(learnedSpells[if(position==0){0}else{position*2}],3)
                        textViewStats.text = spellSpec(learnedSpells[if(position==0){0}else{position*2}],2)
                        textViewDescription.text = spellSpec(learnedSpells[if(position==0){0}else{position*2}],4)
                        clickedSpell = learnedSpells[if(position==0){0}else{position*2}]
                        buttonChoose.visibility = View.VISIBLE
                    }
                }else{
                    viewHolder.button1.isClickable = false
                }
            }catch(e:Exception){
                viewHolder.button1.isClickable = false
                viewHolder.button1.setBackgroundResource(getDrawable(0))
            }
            try{
                if(learnedSpells[if(position==0){1}else{position*2+1}]!=0){
                    viewHolder.button2.setOnClickListener {
                        textViewLabel.text = spellSpec(learnedSpells[if(position==0){1}else{position*2+1}],0)
                        textViewLevel.text = spellSpec(learnedSpells[if(position==0){1}else{position*2+1}],3)
                        textViewStats.text = spellSpec(learnedSpells[if(position==0){1}else{position*2+1}],2)
                        textViewDescription.text = spellSpec(learnedSpells[if(position==0){1}else{position*2+1}],4)
                        clickedSpell = learnedSpells[if(position==0){1}else{position*2+1}]
                        buttonChoose.visibility = View.VISIBLE
                    }
                }else{
                    viewHolder.button2.isClickable = false
                }
            }catch(e:Exception){
                viewHolder.button2.isClickable = false
                viewHolder.button2.setBackgroundResource(getDrawable(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)

    }

    private class ChosenSpellsView(var chosenSpells: Array<Int>) : BaseAdapter() {

        override fun getCount(): Int {
            return 20
        }

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

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

        fun updateAdapter(chsenSpells: Array<Int>) {
            this.chosenSpells = chsenSpells
            notifyDataSetChanged()
        }

        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_chosen_spells, viewGroup, false)
                val viewHolder = ViewHolder(rowMain.button1)
                rowMain.tag = viewHolder
            } else {
                rowMain = convertView
            }
            val viewHolder = rowMain.tag as ViewHolder

            viewHolder.button1.setBackgroundResource(getDrawable(chosenSpells[position]))
            viewHolder.button1.text = (position+1).toString()
            viewHolder.button1.gravity = Gravity.LEFT;

            viewHolder.button1.setOnClickListener {
                chosenSpells[position] = 0
                viewHolder.button1.setBackgroundResource(getDrawable(chosenSpells[position]))
            }

            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 //empty slot
                else -> NULL
            }
                    )
        }

        private class ViewHolder(val button1: TextView)

    }
}

1 个答案:

答案 0 :(得分:0)

Adapter 中使用了 notifyDataSetChanged(),效果很好。