I have an MvxSpinner that is bound to 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" />
The SelectedPhotoCategory
that the SelectedItem is bound to is also a PhotoCategory
. When this screen is in "update mode", the ViewModel sets the SelectedPhotoCategory
to the PhotoCategory whose PhotoCategoryId matches the one in the SQLite database. However, when the spinner is displayed, the default value (which I add to the PhotoCategories
property, PhotoCategory = 0, CategoryName="[Choose a Category]") is shown. The only fix I've found is this (which works ok) code 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've also tried using the GetPosition method of the MvxSpinner.Adapter but this always returns -1 for PhotoCategoryId, CategoryName or SelectedPhotoCategory as the parameter value.
What am I missing??
The binding
should set this for you - and should use
Equals
to find the correct item to select in the spinner.This certainly seems to work in the very latest code when testing using the SpinnerViewModel in https://github.com/slodge/MvvmCross-Tutorials/tree/master/ApiExamples
I know there was a bug reported recently on the use of
==
versusEquals
in one of the bindings - but I don't think this effects the spinner (see https://github.com/slodge/MvvmCross/issues/309).