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.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#6BC6ED"
- >
- <TextView
- android:id="@+id/Name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="25dp"
- android:textColor="#FFFFFF"
- />
- <ListView
- android:id="@+id/demolist"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- </ListView>
- </LinearLayout>
- public class Students {
- public string Name {
- get;
- set;
- }
- public int Age {
- get;
- set;
- }
- }
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.
- public class StudentAdapter: BaseAdapter < Students > {
- public List < Students > sList;
- private Context sContext;
- public StudentAdapter(Context context, List < Students > list) {
- sList = list;
- sContext = context;
- }
- public override Students this[int position] {
- get {
- return sList[position];
- }
- }
- public override int Count {
- get {
- return sList.Count;
- }
- }
- public override long GetItemId(int position) {
- return position;
- }
- public override View GetView(int position, View convertView, ViewGroup parent) {
- View row = convertView;
- try {
- if (row == null) {
- row = LayoutInflater.From(sContext).Inflate(Resource.Layout.Main, null, false);
- }
- TextView txtName = row.FindViewById < TextView > (Resource.Id.Name);
- txtName.Text = sList[position].Name;
- } catch (Exception ex) {
- System.Diagnostics.Debug.WriteLine(ex.Message);
- } finally {}
- return row;
- }
- }
- private ListView studentlistView;
- private List < Students > mlist;
- StudentAdapter adapter;
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- List < Students > objstud = new List < Students > ();
- objstud.Add(new Students {
- Name = "Suresh", Age = 26
- });
- objstud.Add(new Students {
- Name = "C#Cornet", Age = 26
- });
- studentlistView = FindViewById < ListView > (Resource.Id.demolist);
- mlist = new List < Students > ();
- mlist = objstud;
- adapter = new StudentAdapter(this, mlist);
- studentlistView.Adapter = adapter;
- studentlistView.ItemClick += StudentlistView_ItemClick;
- }
- private void StudentlistView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {
- var select = mlist[e.Position].Name;
- Android.Widget.Toast.MakeText(this, select, Android.Widget.ToastLength.Long).Show();
- }



Blake PeavyPosted Jul 12, 2018, 4:28 PM
Must have return type on public StudentAdapter, rather confused by that...
Thamaraiselvan TPosted May 11, 2018, 1:28 AM
Is it possible to add items to an adapter which has items already?
Vishal PrajapatiPosted Feb 14, 2017, 10:40 PM
Nice article...Thanks for sharing...