Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear the selected value from android spinner

Once the check box is check then the spinner will be show and once the check box is unchecked then the spinner will be hidden. I have shown that combination in the below image.

enter image description here

I have achieved this using below code I have shown.

halfHalfCB.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                if (halfHalfCB.isChecked()) {
                    extraDescriptionHalfSP.setVisibility(View.VISIBLE);
                    textview.setVisibility(View.VISIBLE);
                } else {
                    extraDescriptionHalfSP.setVisibility(View.GONE);
                    textview.setVisibility(View.GONE);
//                  String extraDescriptionHalf = extraDescriptionHalfSP
//                          .getSelectedItem() != null ? extraDescriptionHalfSP
//                          .getSelectedItem().toString() : null;
//                          extraDescriptionHalf = null;
                }
            }

        });

        addToCartButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                try {
                    String extraDescriptionHalf = extraDescriptionHalfSP
                            .getSelectedItem() != null ? extraDescriptionHalfSP
                            .getSelectedItem().toString() : null;

Actually my problem is once the check box is checked and then user select a value from spinner and then user unchecked the check box. I want to clear the selected value from the spinner (or make the selected value null). How can I do that? From my current code spinner carries the selected item.

Any help will be highly appreciated.

like image 641
modabeckham Avatar asked Oct 25 '25 17:10

modabeckham


1 Answers

To remove items from the spinner you can use :

myspinner.setAdapter(null);
like image 166
Tharif Avatar answered Oct 27 '25 05:10

Tharif