Quick Start Guide for Xamarin.Android

Create Some Data

So far when we run our project we see little more than an empty grid. It’s about time we added some data! Typically this data will be a collection of ‘data objects’ that are used within your application, this could be a collection of emails, contacts, flight details … all sorts of things! For the purposes of this quick-start guide we’ll create some dummy data, using the theme of Person objects.

Create a Person Object

Locate the root of your project within Xamarin Studio. Right click and select Add > New File…. In the New File prompt, select General and Empty Class, with a name of Person. Click New to create the class. Modify your new Person class so that it looks like the following:

public class Person
{
    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public string name {
        get;
        private set;
    }

    public int age {
        get;
        private set;
    }
}

Create a List of Person Objects

Now we have our Person object we need to instantiate some Persons with real data, and get them into a nice list which our grid can then use.

Back in MainActivity.cs, in the OnCreate method add the following code:

List<Person> people = new List<Person>();
people.Add(new Person("Archie", 25));
people.Add(new Person("Alexander", 15));
people.Add(new Person("Jenny", 42));
people.Add(new Person("Madison", 60));
people.Add(new Person("Joanna", 32));

You’ll also need to add a using statement for the System.Collections.Generic package.

Note: in the QuickStart sample app we use the PersonGenerator class to generate a longer list of Person objects for us but for brevity we've done it manually above!


<< Previous: Create a basic grid | Next: Create some columns >>