Clear Spinner on Button press

[亡魂溺海] 提交于 2020-01-02 11:04:07

问题


Can anyone suggest how to clear the values in the spinner on a button click?

Spinner spinner1;
private static final String[] SpinArray= { "00", "01", "02",
        "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13",
        "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "00" };


adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,
            SpinArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter);

    spinner1.setOnItemSelectedListener(new mySpinnerListener());

public void onItemSelected(AdapterView parent, View v, int position,  
            long id) {
        // TODO Auto-generated method stub
        switch(parent.getId()){
        case R.id.spinner1:
            spinner1=parent.getSelectedItem().toString();
            break;

    }

on button click i tried to clear using

spinner1.setAdapter(null); and spinner1.setOnItemSelectedListener(null);

both of these didn't work in my case.. please help


回答1:


spinner1.getAdapter().clear(); should do the trick.

As Eric said in the comments, call spinner1.getAdapter().notifyDataSetChanged(); after that.




回答2:


Onclicking the button, I cleared the arraylist and make a spinner adapter null.

arrContent.clear();
spinner1.setAdapter(null);


来源:https://stackoverflow.com/questions/11703181/clear-spinner-on-button-press

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