How To Use List View And Adapter In Xamarin Android Application

ListView is one of the most important UI controls of mobile applications, one that is used everywhere from lists of menu options to the list of items. ListView instance requires an Adapter to consume the data contained in row Views.

Let’s see the steps.

Create new Android application.

Add one ListView and one TextView control, to display the list of items and the elements respectively. Use the following code.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:orientation="vertical"  
  4.    android:layout_width="match_parent"  
  5.    android:layout_height="match_parent"  
  6.    android:background="#6BC6ED"  
  7. >  
  8. <TextView  
  9.    android:id="@+id/Name"  
  10.    android:layout_width="match_parent"  
  11.    android:layout_height="wrap_content"   
  12.    android:textSize="25dp"  
  13.    android:textColor="#FFFFFF"  
  14. />  
  15. <ListView  
  16.    android:id="@+id/demolist"  
  17.    android:layout_width="match_parent"  
  18.    android:layout_height="wrap_content">   
  19. </ListView>  
  20. </LinearLayout>  
Create new student class which contains two properties - Name and Age.
  1. public class Students {  
  2.     public string Name {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public int Age {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }  
Next, we need to create Adapter to bind the data to the ListView. Android has in-built base adapter class for Adapter implementations to bind a ListView to a data source. Also, we can get item position, count, and selected item, etc. there. Count method is used to get the number of rows in the list.

GetView method is used to return a View for each row, populated with which element.

GetItemId method is used to return row data properties like row number and value.
  1. public class StudentAdapter: BaseAdapter < Students > {  
  2.     public List < Students > sList;  
  3.     private Context sContext;  
  4.     public StudentAdapter(Context context, List < Students > list) {  
  5.         sList = list;  
  6.         sContext = context;  
  7.     }  
  8.     public override Students this[int position] {  
  9.         get {  
  10.             return sList[position];  
  11.         }  
  12.     }  
  13.     public override int Count {  
  14.         get {  
  15.             return sList.Count;  
  16.         }  
  17.     }  
  18.     public override long GetItemId(int position) {  
  19.         return position;  
  20.     }  
  21.     public override View GetView(int position, View convertView, ViewGroup parent) {  
  22.         View row = convertView;  
  23.         try {  
  24.             if (row == null) {  
  25.                 row = LayoutInflater.From(sContext).Inflate(Resource.Layout.Main, nullfalse);  
  26.             }  
  27.             TextView txtName = row.FindViewById < TextView > (Resource.Id.Name);  
  28.             txtName.Text = sList[position].Name;  
  29.         } catch (Exception ex) {  
  30.             System.Diagnostics.Debug.WriteLine(ex.Message);  
  31.         } finally {}  
  32.         return row;  
  33.     }  
  34. }  
Next, open your activity file and write the below code to create a list of items to display a ListView and show the selected items from the list.
  1. private ListView studentlistView;  
  2. private List < Students > mlist;  
  3. StudentAdapter adapter;  
  4. protected override void OnCreate(Bundle bundle) {  
  5.     base.OnCreate(bundle);  
  6.     // Set our view from the "main" layout resource  
  7.     SetContentView(Resource.Layout.Main);  
  8.     List < Students > objstud = new List < Students > ();  
  9.     objstud.Add(new Students {  
  10.         Name = "Suresh", Age = 26  
  11.     });  
  12.     objstud.Add(new Students {  
  13.         Name = "C#Cornet", Age = 26  
  14.     });  
  15.     studentlistView = FindViewById < ListView > (Resource.Id.demolist);  
  16.     mlist = new List < Students > ();  
  17.     mlist = objstud;  
  18.     adapter = new StudentAdapter(this, mlist);  
  19.     studentlistView.Adapter = adapter;  
  20.     studentlistView.ItemClick += StudentlistView_ItemClick;  
  21. }  
To handle the selected items, write the below code.
  1. private void StudentlistView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {  
  2.     var select = mlist[e.Position].Name;  
  3.     Android.Widget.Toast.MakeText(this, select, Android.Widget.ToastLength.Long).Show();  
  4. }  
Now, run the app and check the output. It should look like the following.



Up Next
    Ebook Download
    View all
    Learn
    View all