In this example we look at how to sort data in Xamarin Android using C# language.We sort data both ascending and descending on button click.

Intro

  • We sort data in ascending and descending manner on button click.
  • We use GridView as our component.
  • We’ve used Visual Studio as our IDE.
  • The code is well commented for easier understanding.

===

Common Questions we answer

With this simple example we explore the following :

  • How o sort data in C#.
  • Sort data ascending and descending manner.
  • How to bind array data to gridview.
  • Using ArrayAdapter with GridView.
  • How to sort and reverse an array in C#.
  • Using Xamarin Android with GridView.

Tools Used

  • IDE : Visual Studio 2013
  • OS : Windows 8.1

Source Code

MainActivity Class

  • Our MainActivity,launcher activity.
  • First we reference views here : in this case our GridView and buttons
using System;
using Android.App;
using Android.Widget;
using Android.OS;

namespace GridView_Sort
{
    [Activity(Label = "GridView Sort", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        //VIEWS
        private GridView gv;
        private Button sortBtn;

        //DATA
        private readonly string[] spacecrafts = { "Kepler", "Casini", "Voyager", "New Horizon", "James Web", "Apollo 15", "Enterprise", "WMAP", "Spitzer", "Galileo" };
        private bool ascending = false;

        //CALLED WHEN ACTIVITY IS CREATED
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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

             sortBtn.Click += sortBtn_Click;

        }

        //SORT BUTTON CLICKED
        void sortBtn_Click(object sender, EventArgs e)
        {
            SortData(ascending);
            this.ascending = !ascending;
        }

        //INITIALIZE VIEWS
        private void InitializeViews()
        {
            gv = FindViewById<GridView>(Resource.Id.gv);
            sortBtn = FindViewById<Button>(Resource.Id.sortBtn);
        }

        //POPULATE GRIDVIEW
        private void Populate()
        {
            gv.Adapter=new ArrayAdapter(this,Android.Resource.Layout.SimpleListItem1,spacecrafts);
        }

        /*
         * SORT
         */
        private void SortData(bool asc)
        {
            //SORT ARRAY ASCENDING AND DESCENDING
            if (asc)
            {
                Array.Sort(spacecrafts);
            }
            else
            {
                Array.Reverse(spacecrafts);
            }

            //CLEAR AND POPULATE LISTBOX
            Populate();

        }
    }

}

Main.axml Layout

  • Main Layout.
  • We specify Views and widgets xml code here.
  • This layout shall get inflated into our mainactivity interface.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#d3d3d3"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <GridView
        android:background="#009968"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="2"
        android:padding="10dp"
        android:id="@+id/gv" />
    <Button
        android:text="Sort"
        android:layout_centerInParent="true"
        android:layout_width="381.0dp"
        android:layout_height="70dp"
        android:padding="10dp"
        android:id="@+id/sortBtn"
        android:layout_marginRight="0.0dp" />
</LinearLayout>

Result

  • Here’s what we get when we run the project.

Xamarin Android GridView Sort Ascending and Descending Xamarin Android GridView Sort Ascending and Descending[/caption]

How To Run

  • Download the project above.
  • You’ll get a zipped file,extract it.
  • Open the Visual Studio.
  • Now close, already open project
  • Now import the project.

More

YouTube

  • Visit our channel for more examples like these.

Facebook

Oclemy,Cheers.

Categorized in: