0

I have a filter checkbox in my spinner to filter where the task is finished or not. If I check an item and check the filter checkbox, the selected value (checked radio button) in the spinner is still checked even the list is already new.

Code Snippet How to uncheck checked item in spinner if filter switch is toggled?


    public class CustomSpinnerAdapter : ArrayAdapter<String> {

        String type = String.Empty;
        List<string> orderList = new List<string>();


        /// <summary>
        /// CUstom Spinner Array Adapter constructor
        /// </summary>
        /// <returns></returns>
        public CustomSpinnerAdapter(Context context, int textViewResourceId, List<String> objects, String type)
                : base(context, textViewResourceId, objects) {
            this.type = type;
            this.orderList = objects;
        }

        /// <summary>
        /// returns the total array count minus one
        /// </summary>
        /// <returns>int</returns>
        public override int Count
        {
            get
            {
                return base.Count;
            }
        }

        public override View GetDropDownView(int position, View convertView, ViewGroup parent)
        {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent)
        {
            LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
            View view = null;

            if (position == 0)
            {
                if(Constants.ORDER_LIST == type) {

                    view = inflater.Inflate(Resource.Layout.CustomSpinnerView, null, true);
                    Switch filterOrder = (Switch) view.FindViewById(Resource.Id.switch_filterOrder);
                    filterOrder.Checked = Constants.ORDER_SWITCH_CLOSED == Constants.ORDER_SWITCH;

                    filterOrder.CheckedChange += delegate (object sender, CompoundButton.CheckedChangeEventArgs e)
                    {
                        saveOrderChecked(e.IsChecked);

                        PageTaskSearchView pageTaskSearchView = new PageTaskSearchView();

                        orderList = pageTaskSearchView.retrieveOrderlist(Constants.ORDER_SWITCH);


                        base.Clear();
                        base.AddAll(orderList);
                        base.NotifyDataSetChanged();

                    };

                }

                else
                {
                    LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(0, 0);
                    view = base.GetDropDownView(position, convertView, parent);
                    TextView tv = (TextView)view;
                    tv.SetHeight(0);
                    tv.LayoutParameters = layoutparams;
                    tv.Visibility = ViewStates.Gone;
                    view = tv;
                }
            }
            else
            {
                view = base.GetDropDownView(position, null, parent);
            }

            parent.VerticalScrollBarEnabled = false;
            return view;

        }

        public static void saveOrderChecked(bool saveChecked)
        {
            Constants.ORDER_SWITCH = saveChecked ? Constants.ORDER_SWITCH_CLOSED : 0;
            Constants.ORDER_SWITCH_TRIGGER = true;
        }
    }
}
        orderSpinner = (Spinner)view.FindViewById(Resource.Id.spinner_workOrder);
        orderListResult = new List<string>();
        clearOrderIDListSelection();
        orderListResult.Clear();
        orderListResult.Add(Constants.EMPTY_STRING);
        foreach (OrderModel orderData in orderIds)
        {
            orderListResult.Add(orderData.IDNo);
        }

        OrderIdAdapter = new CustomSpinnerAdapter(context, Resource.Layout.ListItem, orderListResult, Constants.ORDER_LIST);
        OrderIdAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
        CreateCustomSpinner(orderSpinner, orderListResult, OrderIdAdapter);

Method used to clear spinner selection

public void clearOrderIDListSelection()
        {
            RemoveItemSeclectedEvent(this.orderSpinner);
            CreateCustomSpinner(orderSpinner, orderListResult, OrderIdAdapter);
            orderSpinner.SetSelection(-1);

        }

1 Answers1

0

As what I've found out, spinner is designed to have one of it's items selected, there's no optional api for unselect, doc says the SetSelection method takes in an index that starts from 0, so -1 won't work, you'll need to implement unselect spinner/adapter by your self, but the usual way to do this is just to add a blank item or item that says "None"/"Select One" at the top of the list.

In your code, you can add an empty string as the first element of orderList, and when toggled, just set:

spinner.SetSelection(0)

Also, here's some people's work aroud to set spinner to none.

Lia
  • 523
  • 2
  • 9
  • My spinner has a default radio button property when selected, and I want if I click on filter the checked radio will disappear(Nothing is checked) – Vince Gerald dela Cerna Mar 13 '20 at 10:16
  • Yes, the spinner default is by design, the easiest way is to make the default to blank, but if you really want nothing selected, you need to go with custom renderer, or maybe you can use filter switch + a radio button group instead – Lia Mar 18 '20 at 13:19