Search code examples
c#xamarinmvvmxamarin.androidmvvmcross

Delete item from MvxListView in Xamarin Android using MvvmCross


So I have a list of slides:

SlideListView.axml:

<Mvx.MvxListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:elevation="0dp"
    android:padding="5dp"
    local:MvxItemTemplate="@layout/slidelistitem"
    local:MvxBind="ItemsSource Slides" />

SlideListItemView.axml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/block"
    android:elevation="2dp"
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <EditText
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:text="Test text" />

  <Button
    style="@style/ButtonSlide"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="Delete" />
</LinearLayout>

So each slide has a text and delete button.

SlideListItemViewModel.cs:

public class SlideListItemViewModel : MvxViewModel
{
    private long _id;
    private string _title;

    public long Id { get => _id; set => SetProperty(ref _id, value); }

    public string Title { get => _title; set => SetProperty(ref _title, value); }
}

in SlideListItemViewModel I can't have a constructor because of automapper needs default empty constructor... So the problem is that I need to bind delete command to SlideListViewModel. I don't know how can I do that... What more I have to do so all the delete logic would be inSlideListViewModel and not in SlideListItemViewModel?

UPDATE 1

That's where I create my SlideListItemViewModel

public class SlideListViewModel : MvxViewModel
{
    private readonly IMvxNavigationService _navigation;
    private ICollection<SlideListItemViewModel> _slides;

    public ICollection<SlideListItemViewModel> Slides { get => _slides; set => SetProperty(ref _slides, value); }

    public SlideListViewModel(IMvxNavigationService navigation)
    {
        _navigation = navigation;
    }
}

Solution

  • I will show you how I use in my project and you adapt into your:

    1) Create a class that "wrap" you entity and a command:

    public class EntityWrap<T>
    {
        private Action<T> _realPrimaryCommand { get; set; }
    
        public T Entity { get; set; }
        public ICommand PrimaryCommand { get; set; }
    
        public EntityWrap(T entity, Action<T> realPrimaryCommand)
        {   
            Entity = entity;
            _realPrimaryCommand = realPrimaryCommand;
            PrimaryCommand = new MvxCommand(() => _realPrimaryCommand(entity));
        }
    }
    

    2) My book class:

    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set }
    
        public Book(int id, string name)
        {
            Id = id;
            Name = name;
        }
    }
    

    3) In viewmodel:

    public BooksViewModel : MvxViewModel
    {
        public BooksViewModel()
        {
            var books = new List<Book>() { new Book(1, "AAA"), new Book(2, "BBB"), new Book(3, "CCC") };
            Books = new ObservableCollection<EntityWrap<Book>>(books.Select(x => new EntityWrap<Book>(x, async y => await DoDeleteBookCommand(y))));
        }
    
        private ObservableCollection<EntityWrap<Book>> _books;
        public ObservableCollection<EntityWrap<Book>> Books
        {
            get { return _books; }
            set
            {
                _books = value;
                RaisePropertyChanged(() => Books);
            }
        }
    
        private async Task DoDeleteBookCommand(Book book)
        {
            var bookToRemove = Books.FirstOrDefault(x => x.Entity.Id == book.Id);
            if (bookToRemove != null)
            {
                //Your code...
                Books.Remove(bookToRemove);
            }
        }
    }
    

    4) And in you item layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          local:MvxBind="Text Entity.Name" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Delete"
            local:MvxBind="Click PrimaryCommand" />
    </LinearLayout>
    

    Don't set ItemClick on ListView/RecyclerView unless you have a command for the row click.