ListView And Creating List In Xamarin

Intoduction

In this article we will learn about the ListView in Xamarin. List View is Used to display scrollable list of data. It supports context actions and data biding. We will also see how to add and remove items from the lists. At the end of the article you will have good knowledge about the Lists.

Targeted Audience

People with the basic knowledge of C# and Xamarin.

Tools

Visual Studio with Xamarin Installed.

Components

  • Cells
    Data in ListView is presented in cells. Cells shows a row of data. There are built in cells but you can define custom cells too.
  • TextCell
    Consists of two attributes “Text” and “Detail” to show string of text with a detail of text.
  • Image Cell
    It displays an image with text. It appears as a TextCell with an image on the left side.

The picture below shows a list with a picture and three labels which are being added dynamically. Lets see how to do this.

ListView And Creating List In Xamarin 

First of all right click on your project and add a new folder “Models” to keep your classes separately.

ListView And Creating List In Xamarin 

Add a class name “Contacts” and add four properties like ‘Id’, ’name’, ’address’, ’image’ in it.

ListView And Creating List In Xamarin 

Use below code in the class

C# Code
  1. public class Contacts  
  2.     {  
  3.         private int id;  
  4.         public int Id  
  5.         {  
  6.             get { return id; }  
  7.             set { id = value; }  
  8.         }  
  9.   
  10.         private string name;  
  11.   
  12.         public string Name  
  13.         {  
  14.             get { return name; }  
  15.             set { name = value; }  
  16.         }  
  17.   
  18.         private string address;  
  19.   
  20.         public string Address  
  21.         {  
  22.             get { return address; }  
  23.             set { address = value; }  
  24.         }  
  25.   
  26.         private string image;  
  27.   
  28.         public string Image  
  29.         {  
  30.             get { return image; }  
  31.             set { image = value; }  
  32.         }  
  33.     }  

After this go to your MainPage.xaml.cs and create a propfull with the name of MyList and don’t forget to make this as an “ObservableCollection”, it updates as the changes occurs. In the MainPage() function create the object of your List by initializing it. And add some items for the first time. For now I am adding 10 item by default for your learning purpose. Name the list in the Xaml and set the item source in C# as the MyList.

C# Code
  1. private ObservableCollection<Contacts> myList;  
  2.   
  3.         public ObservableCollection<Contacts> MyList  
  4.         {  
  5.             get { return myList; }  
  6.             set { myList = value; }  
  7.         }  
  8.   
  9.         public MainPage()  
  10.         {  
  11.             InitializeComponent();  
  12.             this.BindingContext = this;  
  13.   
  14.             MyList = new ObservableCollection<Contacts>();  
  15.   
  16.             for (int i = 1; i < 10; i++)  
  17.             {  
  18.                 MyList.Add(new Contacts() { Id = i, Name = "Student" + i.ToString(), Address = "Address" + i.ToString(), Image = "usa.png" });  
  19.             }  
  20.   
  21.             ContactsList.ItemsSource = MyList;  
  22.         }  

Now in XAML create a ListView and give the name to your ContentPage and bind this with the Item Source Of the List. In the header add two buttons to add and delete a item. After that put a tag <ListView.ItemTemplate> and create a <DataTemplate>. Data Template contains the data of the list. Put a ViewCell in it and add Three labels with a image. To add the image just simply copy it and paste outside to your UWP or Android project and add the reference to your tag. And bind each of them with the same name of the property in contacts class.

XAML Code
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:PracApp1"  
  5.              x:Class="PracApp1.MainPage"   
  6.              x:Name="ContentPage1">  
  7.   
  8.     <ListView x:Name="ContactsList" ItemsSource="{Binding Source={x:Reference ContentPage1}, Path=BindingContext.MyList}" IsVisible="True">  
  9.         <ListView.Header>  
  10.             <StackLayout Orientation="Horizontal">  
  11.                 <Button Text="Add New" Clicked="Button_Clicked"></Button>  
  12.                 <Button x:Name="Delete" Text="Delete" Clicked="Delete_Clicked"></Button>  
  13.             </StackLayout>  
  14.         </ListView.Header>  
  15.         <ListView.ItemTemplate>  
  16.             <DataTemplate>  
  17.                 <ViewCell>  
  18.                     <ViewCell.ContextActions>  
  19.                         <MenuItem Text="Delete Item" Clicked="MenuItem_Delete" CommandParameter="{Binding .}">  
  20.                         </MenuItem>  
  21.                     </ViewCell.ContextActions>  
  22.                     <StackLayout Orientation="Horizontal">  
  23.                         <Image Source="{Binding Image}" HeightRequest="50" WidthRequest="50"></Image>  
  24.                         <Label Text="{Binding Id}"></Label>  
  25.                         <Label Text="{Binding Name}"></Label>  
  26.                         <Label Text="{Binding Address}"></Label>  
  27.                     </StackLayout>  
  28.                 </ViewCell>  
  29.   
  30.             </DataTemplate>  
  31.         </ListView.ItemTemplate>  
  32.     </ListView>  
  33. </ContentPage>  

You can see an extra tag of <ViewCell.ContectActions>. it defines the action which will be perform by the item, in this case I put a delete item function on it which will delete the selected item.

ListView And Creating List In Xamarin 
 
ListView And Creating List In Xamarin 

As you can see in the picture the selected item is deleted.

C# Code
  1. private void Button_Clicked(object sender, EventArgs e)  
  2.         {  
  3.             MyList.Add(new Contacts() { Id = 11, Name = "Student", Address = "Address", Image = "usa.png" });  
  4.         }  
  5.   
  6.         private void Delete_Clicked(object sender, EventArgs e)  
  7.         {  
  8.             MyList.Remove(MyList.LastOrDefault());  
  9.         }  
  10.   
  11.         private void MenuItem_Delete(object sender, EventArgs e)  
  12.         {  
  13.             var mi = sender as MenuItem;  
  14.             MyList.Remove((Contacts)mi.CommandParameter);  
  15.         }  

If you click to add button a new item will be added to the list.

ListView And Creating List In Xamarin 

And if you click to the delete the last item will be delete from the list.

ListView And Creating List In Xamarin 
 

Summary

This Article tells you the basics about the ListView and gives you the introuduction about adding and deleting items from the List.


Similar Articles