'AutocompleteTextView item click not firing as excepted

I have an AutoCompleteTextView which behaves like a Spinner, so no possibility of input and dropdown menu when I click on it. When the dropdown menu appear and I click an item for the first time the code fires on SpinnerCapacità_ItemSelected but it exits without firing the statements I need. At the second click everything works perfectly. This is my XML layout:

  <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:weightSum="100"   
 android:orientation="horizontal">
 <com.google.android.material.textfield.TextInputLayout
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="55"      style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu">
  <AutoCompleteTextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:inputType="none"
   android:layout_weight="60"
   android:hint="@string/colorazione"
   android:id="@+id/SpinnerCapacità" />
  </com.google.android.material.textfield.TextInputLayout>
  <com.google.android.material.switchmaterial.SwitchMaterial
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:text="@string/predefinito"
   android:layout_weight="40"
   android:gravity="center"
   android:checked="true"
   android:id="@+id/chbCol"/>
</LinearLayout>

This is SpinnerCapacità_ItemSelected event:

        private void SpinnerCapacità_ItemSelected(object sender, AdapterView.ItemClickEventArgs e) 
    {
        AutoCompleteTextView SpinnerCapacità = (AutoCompleteTextView)sender;
        SwitchMaterial chb = View.FindViewById<SwitchMaterial>(Resource.Id.chbCap);
        var value = Preferences.Get("DefCapacità", "24");
        SpinnerCapacità.ItemClick += (sender, e) =>
        {
            if (value == SelectedTextOfTheDropdownMenu) chb.Checked = true;

            else chb.Checked = false;
        };
    }

And finally this code is inside my OnCreateView

            AutoCompleteTextView SpinnerCapacità = v.FindViewById<AutoCompleteTextView>(Resource.Id.SpinnerCapacità);
        SpinnerCapacità.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs>(SpinnerCapacità_ItemSelected);
        var ArrayCapacità = ArrayAdapter.CreateFromResource(
                Application.Context, Resource.Array.array_capacità, Resource.Drawable.spinner_item);
        ArrayCapacità.SetDropDownViewResource(Android.Resource.Layout.SimpleDropDownItem1Line);
        SpinnerCapacità.Adapter = ArrayCapacità;

Also I don't know in this line if (value == SelectedTextOfTheDropdownMenu) how to get the text of dropdown's selected item. Thanks



Solution 1:[1]

I solved it in this way: Declaration of click event

SpinnerColorazione.ItemClick += SpinnerColorazione_ItemSelected;

Method

private void SpinnerCapacità_ItemSelected(object sender, AdapterView.ItemClickEventArgs e) 
{
    AutoCompleteTextView SpinnerCapacità = (AutoCompleteTextView)sender;
    SwitchMaterial chb = View.FindViewById<SwitchMaterial>(Resource.Id.chbCap);
    var value = Preferences.Get("DefCapacità", "24");

    if (value == e.Id.ToString()) chb.Checked = true;
    else  chb.Checked = false;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Csala