Preview: an ObservableAdapter<T> for Xamarin Android in MVVM Light VNext

.NET, MVVM, Technical stuff, Work, Xamarin
See comments

I don’t know about you, but one of the most annoying things to code in Xamarin Android are adapters for lists. This is a typical example of stuff you need to do over and over again. In MVVM Light VNext, I will include an ObservableAdapter<T> as well as two new extension methods.

With this new class, you will be able to do something like this (for clarity I removed unnecessary code, so this code here does not compile. I will publish a working sample later).

public partial class TestListActivity : Activity
{
    private ObservableAdapter<UserViewModel> _adapter;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.TestList);

        _adapter = Vm.Users.GetAdapter(UpdateTemplate);
        List.Adapter = _adapter;
        List.ItemClick += ListItemClick;
        AddButton.AddCommand("Click", Vm.AddUserCommand);
    }

    private void ListItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        Vm.DeleteUserCommand.Execute(_adapter[e.Position]);
    }

    private View UpdateTemplate(int position, UserViewModel user, View convertView)
    {
        if (convertView == null)
        {
            convertView = LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
        }

        var text = convertView.FindViewById<TextView>(Android.Resource.Id.Text1);
        text.Text = user.FullName;
        return convertView;
    }
}

For the moment, just assume that the List (an Android ListView) and the button named AddButton are found in the UI. In the code above we have the following points worth noting:

  • The Users collection is part of the Activity’s ViewModel, and is an ObservableCollection. An ObservableAdapter’s DataSource can also be an IList, in which case changes will not be observed. If the DataSource is an ObservableCollection, changes to the collection will automatically force the user interface to be reloaded.
  • The ObservableAdapter is created through the GetAdapter extension method which can be called on any ObservableCollection<T> or IList<T>.
  • The VM’s AddUserCommand is an ICommand that will add one user to the Users ObservableCollection. For more info on the AddCommand extension method, see this post.
  • The VM’s DeleteUserCommand is an ICommand that will remove one user from the Users ObservableCollection. It takes the User that must be removed as parameter.
  • The UpdateTemplate method is called from the adapter with enough information to create (if needed) a new “template”, i.e. a new view for the Android list. If no recycled view is passed as parameter of this method, a new view needs to be inflated.
  • This class is not thread safe for now. If the CollectionChanged even is raised on a background thread, the app will crash.

I published an early version of the ObservableAdapter class as a gist. Please feel free to give me your feedback. This class (or a better version of it) will be made available as part of the next version of MVVM Light.

Credit: Frank Krueger’s ObservableTableViewController (for iOS) can be found here.

Previous entry | Next blog entry

Responses to “Preview: an ObservableAdapter<T> for Xamarin Android in MVVM Light VNext”

  1. Preview: an ObservableAdapter for Xamarin Android in MVVM Light VNext Says:

    […] /* 0) { ratingctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating = result[‘Rating’]; SetCurrentRating(‘ctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating_pnlHolder’, result[‘Rating’], “disabled fullStar”, “disabled emptyStar”, true); if(votesCountctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating!=null) { votesCountctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating ++; SetVotesCount(‘ctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating_lblUsersRated’, ‘(‘ + votesCountctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating + ‘ votes)’); } SetRatingCookie(‘r’, ‘i35591′, ‘1’); } else if (result[‘Status’] == 1) { alert(‘The session has expired. Please refresh the page to be able to vote!’); } } /* ]]> */ (0 votes) 0 comments   /   posted by Silverlight Show on May 27, 2014 Tags:   mvvm , xamarin , laurent-bugnion Read original post by Laurent Bugnion at GalaSoft […]

Comments for Preview: an ObservableAdapter&lt;T&gt; for Xamarin Android in MVVM Light VNext