0
  1. I am trying to modify the MvxSpinner to set a default value saying "Select a Value".
  2. I referred this solution to override the methods, but I am not able to override the methods.

How to achieve this in a elegant way


XML

<Mvx.MvxSpinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:background="@drawable/dropdown"
        local:MvxBind="ItemsSource Cruise; SelectedItem CruiseLineSelected; Visible CruiseAndShipSelectionVisibility"
        local:MvxItemTemplate="@layout/spinner_cruise_list"
        local:MvxDropDownItemTemplate="@layout/spinner_cruise_list"
        android:text="Select a cruise"
        android:layout_marginBottom="20dp"
        android:id="@+id/mvxSpinnerCruise" />

Devrath
  • 42,072
  • 54
  • 195
  • 297

1 Answers1

0

I find a simple solution to set a default text for Spinner and it works fine, I think you can use Mvvm to implement this function by yourself, here is the code :

 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 = "";

            TextView b = (view.FindViewById<TextView>(Android.Resource.Id.Text1));
            b.Hint = GetItem(Count).ToString(); //"Hint to be displayed"
        }
        return view;
    }

    public override int Count => (base.Count - 1);
 }

When use the it :

spinner = FindViewById<Spinner>(Resource.Id.spinner);
var adapter = new MySpinnerAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem);

adapter.Add("php");
adapter.Add("java");
adapter.Add("C++");
adapter.Add("C");
adapter.Add("Select a cruise");

spinner.Adapter = adapter;
spinner.SetSelection(adapter.Count); //display hint

Here is the Spinner.

York Shen
  • 9,014
  • 1
  • 16
  • 40