In this class we see how to bind data into a simple ListView using ArrayAdater and handle ItemClicks.

ListView is an adapterview used to render list items.

ListView, as an adapterview can be used with various adapter instances. In this case we use it with ArrayAdapter.

Uses of ListView

To present items in a List format.

Advantages of ListView

No. Responsibility
1. Powerful.
2. Flexible and Customizable.
3. Easy to use.

1. Create Project

  1. Create New Xamarin Android Project.
  2. Choose blank app:

2. Create User Interface

User interfaces in Xamarin Android can be created either imperatively by java code or declaratively by AXML, Microsoft’s subset of XML(eXtensible Markup Language).

(a). Main.axml

This is our MainActivity layout.

Here are it’s roles:

No. Responsibility
1. Define a TextView to render a header/title.
2. Define a ListView to render our items in a List.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#009688">
    <TextView
        android:id="@+id/headerLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:fontFamily="casual"
        android:text="Programming Languages ListView"
        android:textAllCaps="true"
        android:textSize="30sp"
        android:textStyle="bold" />
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/headerLabel"
        android:layout_marginTop="33dp" />
</RelativeLayout>

3. MainActivity.cs

Our MainActivity class.

Here are the roles of this class:

No. Responsibility
1. Allow itself to become a xamarin android activity component by inheriting from Android.App.Activity.
2. Listen to activity creation callbacks by overrding the OnCreate() method.
3. Invoke the OnCreate() method of the base Activity class and pass it a Bundle object we’ve received.
4. Inflate the Main.axml into a View object and set it as the content view of this activity.
5. Maintain a JavaList instance of generic type String as a readonly instance field. This collection will act as our data source.
6. Define a private method FillData() that will populate our JavaList with data.
7. Search for our ListView by it’s id using the FindViewById and assign it to a ListView instance.
8. Instantiate an ArrayAdapter passing in our data source.
9. Set the ArrayAdapter instance as our Adapter property of our ListView.
10. Handle ListView item clicks and show a Toast message of the selected ListView item.
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Widget;
using System;

namespace Simple_ListView
{
    [Activity(Label = "Simple_ListView", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        readonly JavaList<String> languages = new JavaList<string>();

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            //LISTVIEW
            ListView lv = FindViewById<ListView>(Resource.Id.lv);

            //FILL DATA
            FillData();

            //ADAPTER
            ArrayAdapter<String> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, languages);
            lv.Adapter = adapter;

            lv.ItemClick += lv_ItemClick;
        }

        void lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            Toast.MakeText(this, languages[e.Position], ToastLength.Short).Show();

        }

        private void FillData()
        {
            languages.Clear();
            languages.Add("C#");
            languages.Add("Java");
            languages.Add("Cobol");
            languages.Add("C");
            languages.Add("C++");
            languages.Add("Perl");
            languages.Add("Python");
            languages.Add("Ruby");
            languages.Add("Prolog");
            languages.Add("Lisp");

        }
    }
}

Best Regards,
Oclemy.

Categorized in: