0

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

enter image description here

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

enter image description here

 if (_subunits != null && _subunits.Any())
            {
                var currentSubunit = await SettingsService.GetCurrentSubunitAsync();
                if (currentSubunit != null)
                {
                    SelectedSubunit = currentSubunit;
                }
}

enter image description here

enter image description here

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.

CiucaS
  • 2,010
  • 5
  • 36
  • 63
  • Here are a couple of things to try: 1) Change SubunitModel.ToString() to return `$"{Name} ({Id})"` that way you can verify that the Id is what you're expecting. 2) MvxSpinner uses the MvxAdapter to find the SelectedItem in the ItemsSource. Verify that your Equals method is working by writing code like `var pos = Subunits.IndexOf(new SubunitModel { Id = "3", Name = ""});` and make sure pos is correct. – Kiliman Jun 12 '17 at 20:27
  • https://stackoverflow.com/questions/22918100/mvxspinner-selecteditem-not-updated-on-itemssource-change This may be helpful – Tianfu Dai Jun 13 '17 at 01:45
  • @Kiliman var pos = Subunits.IndexOf(new SubunitModel { Id = "3", Name = ""}); returns the right position, but still the spinner doesn't update his position. – CiucaS Jun 23 '17 at 13:01

1 Answers1

0

There is no SelectedItem property on MvxSpinner.

You can bind to HandleItemSelected which gives you the ViewModel bound to the Spinner Item.

So if you change your binding to:

local:MvxBind="ItemsSource Subunits; HandleItemSelected SubunitSelectedCommand"

And add the command to your ViewModel:

private MvxCommand<SubunitModel> _subunitSelectedCommand;
public ICommand SubunitSelected =>
    _subunitSelectedCommand = 
        (_subunitSelectedCommand ?? new MvxCommand<SubunitModel>(OnSubunitSelected));

private void OnSubunitSelected(SubunitModel model)
{
    SelectedSubunit = model;
}
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • There is SelectedItem on MvxSpinner because it works. This still does not answer my problem. It's not a logic problem. It's about the list(view) not refreshing itself with the values from list in ViewModel. – CiucaS Oct 17 '17 at 15:01
  • I guess this was the best answer someone could provide, from your problem description which seems to lack any code supporting it or reproducible steps. – Cheesebaron Oct 17 '17 at 17:17
  • @Cheesebaron: I have a problem, for 0th index, it is taking onlick event degault and want to stop spinner to take onclick for the first time. – GvSharma Feb 06 '18 at 06:37
  • @GvSharma create a new question, this can't be answered in comments. – Cheesebaron Feb 06 '18 at 10:19