How to set the selected item in MvxSpinner

I have an MvxSpinner associated with a List<PhotoCategory> , thus:

 <Mvx.MvxSpinner style="@style/Spinners" android:id="@+id/photoCategorySpinner" android:prompt="@string/photoCategory_prompt" local:MvxBind="ItemsSource PhotoCategories; SelectedItem SelectedPhotoCategory; Visibility ShowPhotoFields, Converter=Visibility" local:MvxDropDownItemTemplate="@layout/spinner_photocategories" local:MvxItemTemplate="@layout/item_photocategory" /> 

SelectedPhotoCategory to which the SelectedItem parameter is PhotoCategory is also PhotoCategory . When this screen is in β€œupdate mode”, the ViewModel sets the SelectedPhotoCategory to PhotoCategory, where PhotoCategoryId matches the one in the SQLite database. However, when the counter is displayed, the default value is displayed (which I add to the PhotoCategories property, PhotoCategory = 0, CategoryName = "[Select Category]"). The only fix I found is the code (which works fine) is added to the view:

 protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.PhotoView); //If we're in Update mode, select the relevant photo category in the spinner: PhotoViewModel photoViewModel = (PhotoViewModel)ViewModel; if (photoViewModel.ScreenMode == Constants.ScreenMode.Update) { MvxSpinner photoCategorySpinner = FindViewById<MvxSpinner>(Resource.Id.photoCategorySpinner); int itemPosition = 0; int selectedPhotoCategoryId = photoViewModel.SelectedPhotoCategory.PhotoCategoryId; foreach (PhotoCategory photoCategory in photoViewModel.PhotoCategories) { if (photoCategory.PhotoCategoryId == selectedPhotoCategoryId) { photoCategorySpinner.SetSelection(itemPosition); } itemPosition++; } } 

I also tried using the GetPosition method for MvxSpinner.Adapter, but this always returns -1 for PhotoCategoryId, CategoryName or SelectedPhotoCategory as the parameter value.

What am I missing?

+4
source share
1 answer

Binding

  SelectedItem SelectedPhotoCategory 

should set this for you - and should use Equals to find the right item to select in the counter.

This certainly works in the latest code when testing using the SpinnerViewModel at https://github.com/slodge/MvvmCross-Tutorials/tree/master/ApiExamples

I know that there has recently been an error related to using == versus Equals in one of the bindings - but I don't think this affects the counter (see https://github.com/slodge/MvvmCross/issues/309 ).

+4
source

Source: https://habr.com/ru/post/1489876/


All Articles