how to clear spinner value in android

喜你入骨 提交于 2020-07-05 04:12:45

问题


i have two spinners when i select items(other then first item) in first spinner which is saved in array it populates data in spinner second which comes from web services. i want that when i again select first data which is title of spinner first it should clear all the value from spinner second. i did all tricks but helpless. suggest me.

my code is:

  if(spinner1== 0) {
        spinner2List.clear();
        ArrayAdapter<String> adapterEmpty = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner2List);
        adapterEmpty.setDropDownViewResource(R.layout.spinner_layout);

        // Apply the adapter to the spinner
        spinner2.setAdapter(adapterEmpty);
    }

thanks.


回答1:


You have called spinner2List.clear(); but haven't notified the adapter by using notifyDataSetChanged(); method.




回答2:


when you select one of your first spinner Items you can use code below in first spinner onItemSelected() method to clear your second spinner.

 Spinner2.setAdapter(null)



回答3:


The Below Code is Used for Clearing the Spinner Value:-

ArrayList<String> allAreaNames;

                allAreaNames.clear();

                ArrayAdapter<String> AreaAdapter = new ArrayAdapter<String>
                        (context, android.R.layout.simple_spinner_item, allAreaNames);


                AreaAdapter.setDropDownViewResource
                        (android.R.layout.simple_spinner_dropdown_item);

                AreaSpinner.setAdapter(AreaAdapter);


来源:https://stackoverflow.com/questions/14706497/how-to-clear-spinner-value-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!