Skip to content

Lots of Lists: Part 1, Simple List Activity

In most mobile applications, you’re going to be presenting your users a list of something. Most of the time, it’s not as simple as an array of strings; your data may be stored away in a local Sqlite database, or perhaps behind a RESTful API. In any case, we’ll be taking a look at what it takes to begin with a simple ListActivity that can be easily updated later on.

Create a new Android Project (1.1+) and name the activity MyListActivity (or whatever you prefer.) Run the app to ensure you’re working off a valid clean slate.

Next, modify your activity (MyListActivity) to extend the Android class ListActivity and adjust your code to look like the following:

package com.learnandroid.listviewtutorial.simple;
 
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
 
public class MainListView extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        ListAdapter adapter = createAdapter();
        setListAdapter(adapter);
    }
 
    /**
     * Creates and returns a list adapter for the current list activity
     * @return
     */
    protected ListAdapter createAdapter()
    {
    	return null;
    }
}

So far, so good. We have a method to create our list adapter, so essentially, our onCreate() method is done. Let’s move on to adapters.

Adapters basically glue a collection of objects to an activity, usually by providing a mapping or instructions on how to render each object in the list for the activity. In our case, we have a simple list of strings, so our adapter is pretty straightforward (and in fact, is already provided by Android: ArrayAdapter)

Modify your createAdapter() method to look like the following:

    /**
     * Creates and returns a list adapter for the current list activity
     * @return
     */
    protected ListAdapter createAdapter()
    {
    	// Create some mock data
    	String[] testValues = new String[] {
    			"Test1",
    			"Test2",
    			"Test3"
    	};
 
    	// Create a simple array adapter (of type string) with the test values
    	ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, testValues);
 
    	return adapter;
    }

This encapsulates everything we need to get an adapter up and running. Typically here we may make a call-out to data, which may be a Sqlite query or a RESTful GET request, but for now we’re using simple strings. We create an ArrayAdapter and specify three things: Context, Layout, and Data.

The Context is simply where the list came from. In our app it’s simple, but sometimes it can get a little complex when you’re passing intents back and forth across activities, so this is used to keep a reference to the owner activity at all times.
The Layout is where the data will be rendered (and how it will look.) We’re using a built-in Android layout for our needs (simple_list_item_1).
The Data is what will be rendered, and is a simple list of strings.

Final result should look like the following:

package com.learnandroid.listviewtutorial.simple;
 
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
 
public class MainListView extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        ListAdapter adapter = createAdapter();
        setListAdapter(adapter);
    }
 
    /**
     * Creates and returns a list adapter for the current list activity
     * @return
     */
    protected ListAdapter createAdapter()
    {
    	// Create some mock data
    	String[] testValues = new String[] {
    			"Test1",
    			"Test2",
    			"Test3"
    	};
 
    	// Create a simple array adapter (of type string) with the test values
    	ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, testValues);
 
    	return adapter;
    }
}

Run the app, and you should see something like this:

A screen shot of an example list of strings

A screen shot of an example list of strings

And there you have a nice introduction to a ListActivity that can be expanded upon by focusing on the createAdapter() method. In Part 2, we’ll explore using more complex Object lists and custom Adapters, as well as a brief introduction to the layout files. Thanks for reading!

{ 10 } Comments

  1. RT | May 7, 2010 at 1:23 pm | Permalink

    Thank you, that was really helpful!

  2. Unq | July 16, 2010 at 4:50 pm | Permalink

    Hi, I find your posts very clear and easy to follow. I have one question about this one. I get “createAdapter cannot resolve to a type” error when trying out this code. Am I missing something?

  3. Unq | July 16, 2010 at 4:57 pm | Permalink

    I don’t know what was wrong. I wrote the code again and it works. Thanks for the tutorials. :)

  4. Thiago | October 27, 2010 at 10:02 am | Permalink

    When the part 2 will be released? Thanks

  5. Norton | November 21, 2010 at 4:48 am | Permalink

    Hi,

    I am new to Android development. I would wanna start a List Activity from an activity. How do i go about it? My Manifest file is as below:

    NotificationListActivity is what i would wanna start from my Activity class. Could some one help me with this.

    Regards,
    Norton

  6. Recca_kiko | March 25, 2011 at 2:32 am | Permalink

    These r awesome… It’d be even grt if u continue to offer even more ex’s n links …

  7. Anon | April 19, 2011 at 12:49 pm | Permalink

    I’ve seen this everywhere how do I use simple_list_2 is there an example of that?

  8. misu | June 25, 2011 at 5:46 am | Permalink

    Hey man it is’nt wotking,It say’s ‘Acess ‘ to calss not allowed!

    Don’t u ned a layout to define or something?

  9. KUMARESAN | September 18, 2011 at 7:21 am | Permalink

    Wat if ‘ListView’ is in custom designed layout. I mean, Layout is not just listview. its collection of listview, with buttons at the button and top.

  10. Kenley | November 8, 2011 at 10:50 pm | Permalink

    I’ve updated the post to fix a typo and will be picking this tutorial series back up. Thanks for reading!

{ 4 } Trackbacks

  1. Learn Android : Android Layout Tutorial | January 9, 2010 at 9:47 am | Permalink

    [...] also created a demo project that uses the code samples from this tutorial and code samples from the Lots of Lists: Part 1, Simple List Activity tutorial. You can download the demo source code [...]

  2. Android Layout Tutorial | developersmania | September 12, 2011 at 6:05 am | Permalink

    [...] have also created a demo project that uses the code samples from this tutorial and from the Lots of Lists: Part 1, Simple List Activity tutorial. You can download the demo source code [...]

  3. [...] other classes we can use, that also inherit from Activity to provide us different functionality. In this article we used a ListActivity to present a list to the user. In fact, let’s go ahead and make the [...]

  4. [...] Part 1 of Lots of Lists, we examined how easy it is to create a screen that shows a list. However, real-world lists are [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *