So I have the following situation.
I have an MvxSpinner
<MvxSpinner
android:id="@+id/spinnerSubunit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="sans-serif"
android:textSize="16sp"
local:MvxBind="ItemsSource Subunits;SelectedItem SelectedSubunit;" />
My models look like this
private IList<SubunitModel> _subunits;
public IList<SubunitModel> Subunits
{
get { return _subunits; }
set
{
_subunits = value;
RaisePropertyChanged(() => Subunits);
}
}
private SubunitModel _selectedSubunit;
public SubunitModel SelectedSubunit
{
get { return _selectedSubunit; }
set
{
_selectedSubunit = value;
RaisePropertyChanged(() => SelectedSubunit);
OnSubunitSelected();
}
}
public class SubunitModel
{
public string Id { get; set; }
public string Name { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
return Id == ((SubunitModel)obj).Id;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return Name;
}
}
And I found a strange behaviour in a specific case.
For example we take 2 lists
Here we select the 3rd element. (Let's say the id for this element is 3)
After I select an element it is saved in db.
Now if we change the list, so it has more elements, we make sure it still contains the same elemented selected in the first list. ( see picture 2 ). As you can see it has the same element, but it's position is changed ( it's not the 3rd element anymore ).
After we get the list we call a method, that takes the SelectedSubunit from the DB, check if the selected subunit exists in the current list and if it does it marks it as SelectedSubunit
if (_subunits != null && _subunits.Any())
{
var currentSubunit = await SettingsService.GetCurrentSubunitAsync();
if (currentSubunit != null)
{
SelectedSubunit = currentSubunit;
}
}
And finally the problem:
as you can see from picture 3 and 4. In the new list ( the bigger one) the selected element shown in the View is the 3rd ( not the one saved in the DB ).
For some reason the binding is lost somewhere. When I check with the debugger. SelectedSubunit has the right value which is 3, "Sediu Central", but on the spinner the item shown as selected is X, Arcade(Real).
I've tried with multiple lists, the same result. If the SelectedItem is found in any of them, the view doesn't update the spinner, it shows as the 3rd item is the selected one.
Hope I made myself clear, it's so hard to explain the situation.