How to Display New Item on MvxListView on Button Click in Android Xamarin Visual Studio

338 Views Asked by At

I want to display a new line of TextView everytime I click a button. I am using MvvmCross and I know that I should use MvxListView which is assigned to a layout template, but I do not know how to display it on screen on button click.

Can someone give me a simple code example of doing this from ViewModel and very short explanation about which line of code that initiate the display of new TextView?

UPDATE

I provide my code below:

ViewModel.cs

public class FirstViewModel 
    : MvxViewModel
{
    private ObservableCollection<Unit> unitCodes;
    public ObservableCollection<Unit> UnitCodes
    {
        get { return unitCodes; }
        set { unitCodes = value; RaisePropertyChanged(() => UnitCodes); }
    }

    public IMvxCommand ButtonCommand { get; private set; }
    public FirstViewModel()
    {
        unitCodes = new ObservableCollection<Unit>();
        ButtonCommand = new MvxCommand(() =>
        {
            UnitCodes = UnitCodes ?? new ObservableCollection<Unit>();
            UnitCodes.Add(new Unit("123", "Test Name"));
        });
    }

    public class Unit : MvxViewModel
    {
        private string unitCode;
        public string UnitCode
        {
            get { return unitCode; }
            set { unitCode = value; RaisePropertyChanged(() => UnitCode); }
        }
        private string unitName;
        public string UnitName
        {
            get { return unitName; }
            set { unitName = value; RaisePropertyChanged(() => UnitName); }
        }

        public Unit() { }
        public Unit(string unitCode, string unitName)
        {
            UnitCode = unitCode;
            UnitName = unitName;
        }
    }
}

View.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        local:MvxBind="Click ButtonCommand"
        android:text="Click To Add" />
    <Mvx.MvxListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        local:MvxItemTemplate="@layout/unitcodeitemlayout"
        local:MvxBind="ItemSource UnitCodes" />
</LinearLayout>

unitcodeitemlayout.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        local:MvxBind="Text UnitName" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        local:MvxBind="Text UnitCode" />
</LinearLayout>

My code does not have any compilation or run-time error, however there is nothing happening on button click. What I want is to add "123" and "Test Name" to the display every time I click the button.

2

There are 2 best solutions below

1
Darren Christopher On BEST ANSWER

Your code is right. However, you misspelled the ItemsSource under MvxListView axml.

It should be:

<Mvx.MvxListView
     ...
     local:MvxBind = "ItemsSource UnitCodes"/>
1
wishmaster On

In order to set the ListView, take a look at this MvvmCross tutorial

You could end up with something like this:

ViewModel:

public class MyListViewModel : MvxViewModel
    {
        private ObservableCollection<TextViewCellViewModel> _listItems;

        public virtual ObservableCollection<TextViewCellViewModel> ListItems {
            get {
                return _listItems;
            }
            set {
                _listItems = value;
                RaisePropertyChanged(() => ListItems);
            }
        }

        private IMvxCommand _addTextViewCommand;

        public IMvxCommand AddTextViewCommand {
            get {
                _addTextViewCommand = _addTextViewCommand ?? new MvxCommand(AddTextView);
                return _addTextViewCommand;
            }
        }


        private void AddTextView()
        {
            ListItems = ListItems ?? new ObservableCollection<TextViewCellViewModel>();

            ListItems.Add(new TextViewCellViewModel());
        }


        public class TextViewCellViewModel : MvxViewModel
        {
            private string _text;
            public string Text { get { return _text; } set { _text = value; RaisePropertyChanged(() => Text); } }

            public TextViewCellViewModel() { }
        }
    }

Android ListView Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="fill_vertical"
    android:paddingRight="20dp"
    android:paddingLeft="20dp">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/frameLayout"
        android:paddingBottom="50dp">
        <MvxListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scrollbars="vertical"
            android:id="@+id/list_view_tripplanner_result"
            android:divider="@android:color/transparent"
            android:paddingTop="10dp"
            android:clipToPadding="false"
            android:dividerHeight="7dp"
            android:descendantFocusability="afterDescendants"
            local:MvxItemTemplate="@layout/cell_textview"
            local:MvxBind="ItemsSource ListItems;" />
    </FrameLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:paddingTop="5dp"
        android:paddingBottom="10dp">
        <Button
            android:layout_width="match_parent"
            android:layout_height="40dp"
            local:MvxBind="Click AddTextViewCommand"
            android:text="Add TextView"
            android:textColor="@color/primary_white"
            android:background="@android:color/darker_gray" />
    </LinearLayout>
</RelativeLayout>

Which should look like this: (notice button on the bottom of the Listview) enter image description here

Where @layout/cell_textview would look something like this:

layout for the ItemTemplate for the ListView would be:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:textSize="20dp"
        local:MvxBind="Text Text"/>
</LinearLayout>