在Android中执行OnItemSelectedListener时管理具有相同数据的微调器

时间:2018-07-13 06:21:57

标签: android kotlin spinner baseadapter onitemselectedlistener

我有5个微调器。我正在使用BaseAdapter在微调器上设置自定义行。我正在每个Spinner中加载相同的数据,即模型的ArrayList。

现在,问题是,当我从任何微调器中选择一个项目时,所选项目应从其他微调器中消失。或者,如果我取消选择它或将以前选择的项目更改为另一个,则它应该重新出现在其他微调器中。

下面是我的代码

class ConsumerPreferencesActivity : BaseActivity() {

    private lateinit var mainList: ArrayList<Cuisine>
    private lateinit var selectedList: ArrayList<Cuisine>
    private lateinit var displayList: ArrayList<Cuisine>

    override fun onCreate(savedInstanceState: Bundle?) {

        mainList = ArrayList()
        displayList = ArrayList()
        selectedList = ArrayList()

        for (i in 1..5) {
            selectedList.add(Cuisine())
        }
        mainList.add(Cuisine())     //for empty first row
        displayList.add(Cuisine())  //for empty first row

        cuisinesSpinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View, position: Int, id: Long) {
                selectedList[0] = displayList[position]
                doIterate()
            }
        }

        cuisinesSpinner2.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View, position: Int, id: Long) {
                selectedList[1] = displayList[position]
                doIterate()
            }
        }

        cuisinesSpinner3.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View, position: Int, id: Long) {
                selectedList[2] = displayList[position]
                doIterate()
            }
        }

        cuisinesSpinner4.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View, position: Int, id: Long) {
                selectedList[3] = displayList[position]
                doIterate()
            }

            override fun onNothingSelected(parentView: AdapterView<*>) {
            }
        }

        cuisinesSpinner5.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View, position: Int, id: Long) {
                selectedList[4] = displayList[position]
                doIterate()
            }
        }

    }

    private fun doIterate() {
        displayList.clear()
        displayList.addAll(mainList)

        for (c in selectedList) {

            for (d in mainList) {

                if (c.name != null) {
                    if (c.name == d.name) {
                        displayList.remove(c)
                    }
                }
            }
        }
        Log.e("TAG", "DATA")
    }

请帮助我解决此问题。谢谢

0 个答案:

没有答案