MvvmCross: What is the best way to get the selected item from an MvxListView in android?

JohnB picture JohnB · May 22, 2013 · Viewed 7.1k times · Source

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.

Answer

Stuart picture Stuart · May 22, 2013

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