选择后,Android微调器getDropDownView重复项目

我有下面的SpinnerAdapter,它成功地为我的对象Discipline列表,它的正确的布局样式的微调和下拉项目。 但是,当我选择其中一个项目时,位置0的文本会在选定的项目位置上重复。

class ClassesSpinnerAdapter(context: Context, resourceId: Int, val disciplineClasses: List<Discipline>) : ArrayAdapter<Discipline>(context, resourceId, disciplineClasses) { override fun getItem(position: Int): Discipline { return disciplineClasses[position] } override fun getCount(): Int { return disciplineClasses.size } override fun getItemId(position: Int): Long { return super.getItemId(position) } override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { var holder: ClassSpinnerHolder var rowView: View if (convertView == null) { rowView = LayoutInflater.from(parent?.context).inflate(R.layout.classes_spinner_row, parent, false) holder = ClassSpinnerHolder() holder.classNameTxt = rowView.findViewById(R.id.classes_spinner_text) as TextView? holder.classNameTxt?.text = disciplineClasses[position].className.replace("ano", "") rowView.tag = holder } else { holder = convertView.tag as ClassSpinnerHolder rowView = convertView } val discipline: Discipline = disciplineClasses[position]; if (discipline != null) { holder.classNameTxt?.text = disciplineClasses[position].className.replace("ano", "") } return rowView } override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View { var holder: ClassSpinnerHolder var rowView: View if (convertView == null) { rowView = LayoutInflater.from(parent?.context).inflate(R.layout.classes_spinner_row, parent, false) holder = ClassSpinnerHolder() holder.classNameTxt = rowView.findViewById(R.id.classes_spinner_text) as TextView? holder.classNameTxt?.text = disciplineClasses[position].className.replace("ano", "") rowView.tag = holder } else { holder = convertView.tag as ClassSpinnerHolder rowView = convertView } return rowView } inner class ClassSpinnerHolder() { var classNameTxt: TextView? = null } 

这是我得到的结果:

初始状态

第一下拉

选择第二个元素后下拉

我有点解决它通过删除getDropDownView()方法和重写toString我的模型类,但我想要一个更好的解决方案,我想我重写getDropDownView()在一个错误的方式,有人可以帮我吗?

getDropDownView() ,只更新if (convertView == null)大小写的视图的值。 每次调用该方法时,都应该更新所有的值,否则回收的视图将会有垃圾。

Interesting Posts