Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 214
JohnB Avatar asked May 22 '13 17:05

JohnB


People also ask

Can mvvmcross use ViewModel as a source for ICommand?

With the method binding plugin, MvvmCross data binding can use ViewModel public methods as sources for ICommand without declaring an ICommand property. can be used in binding as:

What is the use of mvvmcross?

MvvmCross is a library to enable usage of the Mvvm pattern in Xamarin apps when targeting iOS and Android. With this library it’s really easy to share viewmodels between the apps instead of creating separate for each platform. It also enables bindings on Android and iOS.

What is the best cross-platform MVVM library?

MvvmCross to the rescue! MvvmCross is a cross-platform MVVM library that allows you to put a common PCL directly from xamarin.android and xamrin.ios. And you get most of the Mvvm data binding to view sugars around this too.

What is the difference between Xamarin and mvvmcross?

Xamarin comes both as a standalone studio or you could use Visual Studio. MvvmCross is a library to enable usage of the Mvvm pattern in Xamarin apps when targeting iOS and Android. With this library it’s really easy to share viewmodels between the apps instead of creating separate for each platform.


1 Answers

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:

  • DailyDilbert - https://github.com/slodge/MvvmCross-Tutorials/blob/master/DailyDilbert/DailyDilbert.Droid/Resources/Layout/ListView.axml
  • OldTutorial - https://github.com/slodge/MvvmCross-Tutorials/blob/master/Sample%20-%20Tutorial/OldTutorial/Tutorial.UI.Droid/Resources/Layout/Page_MainMenuView.axml
  • N=16 - Collect-A-Bull - see http://youtu.be/1PC5Q30FyR4?t=26m10s and https://github.com/slodge/NPlus1DaysOfMvvmCross/blob/master/N-16-CollectABull-Part5/CollectABull.Droid/Resources/Layout/ListView.axml

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

like image 106
Stuart Avatar answered Sep 27 '22 16:09

Stuart