kotlin Android spinner notifyDataSetChanged()

时间:2018-05-25 07:17:40

标签: android dynamic kotlin spinner

我想动态更新spinner2,所以我使用函数notifyDataSetChanged()。但它不起作用,请帮助。

这里有代码。选择了spinner1后,spinner2的数组内容发生了变化。然后它假设动态更新spinner2,但它没有。

     spinnerOne = findViewById(R.id.spinner1) as Spinner
        spinnerTwo = findViewById(R.id.spinner2) as Spinner

        //result = findViewById(R.id.tv_result) as TextView

        val options = arrayOf("111", "222", "333", "222", "333", "222", "333", "222", "333", "222", "333", "222", "333", "222", "333", "222", "333", "222", "333", "222", "333", "222", "333", "222", "333")
        val taiwan = arrayOf("xxx","yyy","zzz")
        var tempArray = options

        //spinnerOne.adapter = ArrayAdapter<String>(this,R.layout.item,options)
        spinnerOne.adapter = ArrayAdapter<String>(this, R.layout.item, options)

        var dataAdapter = ArrayAdapter<String>(this,  R.layout.item, tempArray)

        spinnerTwo.setAdapter(dataAdapter)
//        dataAdapter.notifyDataSetChanged()



        spinnerOne.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {
                //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.


            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

                tempArray = taiwan
                for (element in tempArray) {
                    println(element)
                }
                dataAdapter.notifyDataSetChanged()
            }
        }




        spinnerTwo.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {
                //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.


                //result.text = options.get(position)
            }
        }

enter image description here

2 个答案:

答案 0 :(得分:1)

您没有更改适配器内容 - 您只需设置新的变量tempArray值。 tempArray = taiwan调用适配器仍保留相当于options变量的数据。

<强>被修改

很抱歉第一个代码示例 - 我不知道ArrayAdapter不支持更改内容。我从来没有为自己编辑过。 :)我经常使用自定义适配器 - 它也可以为您提供解决方案。

最好使用MutableList类型制作tempArray变量。然后改变它的内容。

tempArray.clear()
tempArray.addAll(taiwan)

此代码更改了适配器引用的集合内容,因此适配器可以看到它。在你的代码中只是在变量中切换引用。

毕竟不要忘记notifyDataSetChanged

答案 1 :(得分:0)

我根据区域值集选择微调器城市值时为微调器城市和区域提供一个简单的java代码方法。你可以设置删除执行任何操作。

   private void setSpinser() {
    spCity = findViewById(R.id.city);
    spRegion = findViewById(R.id.reigon);
    List<String> cityName = new ArrayList<>();
    final List<String> regionName = new ArrayList<>();
    final ArrayAdapter<String> regionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    cityName.add("Select city");
    cityName.add("Ahmadabad");
    cityName.add("Surat");
    cityName.add("Delhi");
    ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(DbInsert.this, android.R.layout.simple_list_item_1, cityName);
    spCity.setAdapter(stringArrayAdapter);
    spCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            switch (spCity.getSelectedItem().toString())
            {
                case "Ahmadabad":
                    regionName.add("Abc");
                    regionName.add("cde");
                    regionName.add("efg");
                    regionAdapter.addAll(regionName);
                    spRegion.setAdapter(regionAdapter);
                    regionAdapter.notifyDataSetChanged();
                    break;
                case "Surat":
                    regionName.add("pqr");
                    regionName.add("stu");
                    regionName.add("xyz");
                    regionAdapter.addAll(regionName);
                    spRegion.setAdapter(regionAdapter);
                    regionAdapter.notifyDataSetChanged();
                    break;

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });