How to make clear(empty/without values) spinner on clearbutton
Asked Answered
W

5

6

I'm using a spinner in my apps so i need a button reset .on click on clearButton button spinner should clear (no value should be displayed )

spinner=  (Spinner) view.findViewById(R.id.mylistspinner);

list= new ArrayList<String>();

list.add("");

list.add("1");        
list.add("2");

adapter= new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text,list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(urineGlucoseAdapter);
        spinner.setOnItemSelectedListener(new OnUGItemSelected());
Wrongheaded answered 15/6, 2012 at 5:0 Comment(4)
"spinner should clear", does that mean the spinner should reset to the first item, or should all spinner values be cleared?Bayadere
Spinner will always show some item from the array of input entries that you are using to fill it up. If you don't want to show any entry in the spinner you might want to add one value at [0] position with is an empty string "".Thyroiditis
if you are using custom adapter with arrayList. then remove all items from arraylist and call notifydatasetchanged().Ubana
sana/christine ..I have 10 values in my spinner but according to my apps need .i ll hv to add clear button .on click of that it should make clear/or with out valuse like (clear button for date and time)Wrongheaded
O
14

two way u can do it on click of button use either

 arr.clear();
 spinner.setAdapter(null);  

or

 arr.clear();
 spinner.setAdapter(new ArrayAdapter<String>(YourActivity.this,android.R.layout.simple_dropdown_item_1line,arr)); 
Orient answered 15/6, 2012 at 5:36 Comment(0)
P
3
units = new String[0];
Arrays.fill(units, null);
Log.i("units array length", ""+units.length);
unit_adapter = new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_spinner_item, units);
spinner.setAdapter(unit_adapter);
// unit_adapter.notifyDataSetChanged();
Parson answered 26/9, 2013 at 6:21 Comment(0)
U
2

on button click do this

list.clear();
adapter.notifyDataSetChanged();
Ubana answered 15/6, 2012 at 5:42 Comment(0)
B
0

If you want to delete all spinner items:

mySpinner.setAdapter(null);
Blackcock answered 15/6, 2012 at 5:31 Comment(2)
first remove list items ..list.removeAll(list)Blackcock
and then set adapter to spinner againBlackcock
C
-1

You can call adapter.clear(); to remove all items from spinner. Then pass an arraylist again to populate the spinner with new values.

Chiseler answered 26/7, 2016 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.