7

I'm using MvvmCross v3.06 and I've defined a MvxListView in android which is bound to a list. I can see the list but can't work out the best way to get hold of the item that is selected when I click it.

At the moment I'm doing the following in the OnCreate of the activity but it's not particularly MVVM and I wondered if there is a better way through binding?

var list = FindViewById<MvxListView>(Resource.Id.subslist);
list.ItemClick = ((MyViewModel)ViewModel).ItemSelectedCommand;

I can't find any documentation on the best way to go about this so any help would be great.

Stuart
  • 66,722
  • 7
  • 114
  • 165
JohnB
  • 117
  • 1
  • 5

1 Answers1

13

For android, the most common technique is to bind ItemClick to an MvxCommand<TItem> - so use:

 local:MvxBind="ItemClick ItemSelectedCommand"

You can see this in operation in examples including:


Less common (so far), for Android, you can also bind to a custom binding SelectedItem on MvxListView

This technique is shown on a Spinner (MvxSpinner) in MoreControls - https://github.com/slodge/MvvmCross-Tutorials/blob/master/MoreControls/MoreControls.Droid/Resources/Layout/FirstView.axml (this example is constructed live during the N=18 video - see http://youtu.be/s1LhXdCTsn4?t=7m26s

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • Excellent, that's really useful. Many thanks Stuart, and compliments on your work on MvvmCross. – JohnB May 23 '13 at 10:45
  • I'm having trouble getting the SelectedItem feature to work for a MvxListView. I can see the databinding working, but the selected item highlights momentarily, and then the highlight disappears. Any ideas why? – jokeane Oct 01 '13 at 18:22
  • 1
    See http://stackoverflow.com/a/5058401/373321 for why Android discourages selection... and http://stackoverflow.com/questions/18364907/how-to-highlight-the-selected-item-in-an-mvxlistview/18409625#18409625 for one user's approach – Stuart Oct 01 '13 at 19:30