I have developed an application in which i have one edit text value of edit text is entered in spinner on button clicked an the item i select to delete gets deleted but the
For me the accepted answer throwed an exception so what I did was this.
ArrayAdapter adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, Collections.emptyList());
spinner.setAdapter(adapter);
I had the same problem. Doing this did solve the problem, by emptying the spinner:
mySpinner.setSelection(Adapter.NO_SELECTION);
for delete all items use:
mySpinner.setAdapter(null);
if you want to delete a particular item, remove this from m_adapterForSpinner and then:
mySpinner.setAdapter(m_adapterForSpinner);
Remember: methods "remove" and "clear" do not work with ArrayAdapter for Android 2.3.3 and lower. You'll receive error message "Unsupported operation.... bla bla bla". May be they work with Android 4.0 and up, I don't know. Here is my solution which works even with minSdkVersion="5".
Create Spinner and fill it with test data This code is in an activity form. Use ArrayList as data source for ArrayAdapter instead of array. It's important because you'll use its "remove" method later
List<String> reportFiles = new ArrayList<String>();
//fill some test data
reportFiles.add("aaaaaaaaaaaaa");
reportFiles.add("bbbbbbbbbbbbb");
reportFiles.add("ccccccccccccc");
//create spinner from corresponding layout xml file
Spinner spnReport = (Spinner) findViewById(R.id.spinner_report);
//create ArrayAdapter
ArrayAdapter reportAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, reportFiles);
//set adapter to spinner
spnReport.setAdapter(reportAdapter);
//select first item in adapter - is not obligatory
spnReport.setSelection(0);
Remove selected item from Spinner
reportFiles.remove((String)spnReport.getSelectedItem());
//create new ArrayAdapter with modified ArrayList
reportAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,reportFiles);
//set new array adapter to spinner
spnReport.setAdapter(reportAdapter);
//select first item
spnReport.setSelection(0);