Remove Item from Drop Down Adapter

EnoharaEnohara Member ✭✭✭
edited September 2018 in Xamarin.Android

I want to remove the first item on my drop down in spinner.
Do you guys have any idea how to do it?

Thanks in advance :)

Answers

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai
    edited September 2018

    @Enohara

    You need to create a custom adapter, for example:

    public class MySpinnerAdapter<T> : ArrayAdapter<T>
    {
        private Context mContext;
        private int mTextResourceId;
    
        public MySpinnerAdapter(Context context, int textViewResourceId) : base(context, textViewResourceId)
        {
            mContext = Context;
            mTextResourceId = textViewResourceId;
        }
    
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = base.GetView(position, convertView, parent);
            if (position == Count)
            {
                TextView a = (view.FindViewById<TextView>(Android.Resource.Id.Text1));
                a.Text = "";
                a.Hint = GetItem(Count).ToString();                
            }
            return view;
        }
        public override int Count => (base.Count - 1);
    }
    

    You could use it by:

            Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner1);
            var adapter = new MySpinnerAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem);
            adapter.Add("AAA");
            adapter.Add("BBB");
            adapter.Add("CCC");
            adapter.Add("DDD");
            adapter.Add("Select a value");
            spinner.Adapter = adapter;
            spinner.SetSelection(adapter.Count);
    

    And the result:

    https://stackoverflow.com/questions/44343666/how-to-set-a-default-value-for-mvxspinner/44383396#44383396

Sign In or Register to comment.