ListViews

Today I am going to cover Lists. Lists can be made several ways, today I will show you how to make a static text based list. After we are displaying the list I will then show you how to send the text from the list to a Toast (popup) when the item is clicked.

Lets start with the XML.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/fulllist"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
/>
</LinearLayout>

This sets up a basic ListView widget to hold the list data and display it to the screen.

Now we want to make an Activity like normal but for this example your class should extend ListActivity. We will then create a list of movie monsters.

So speaking of… Here is the list of monsters.

static final String[] MONSTERS = new String[] { "Zombie", "Vampire", "Warewulf", "Blob" };

Now for the basic onCreate method:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

Now we need to make an adapter to hold the monsters. After the super call add:

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MONSTERS));

android.R.layout.simple_list_item_1 is a default xml view for displaying a list. This can be customized for a variety of list displays.

Now lets make a ListView object that links to the XML widget:

ListView lv = getListView(); // Grabs the ListView widget that is tied to the Activity.
lv.setTextFilterEnabled(true); // Enable the Text Filter. Typing when this view has focus will filter the children to match the users input

Now we are going to create the click listener and the onItemClick event

lv.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        // Do Stuff Here
    }
});

Lastly we will add the Toast call, put this in the onItemClick stub:

Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();

Here is the whole Class:

public class Lister extends ListActivity {
	static final String[] MONSTERS = new String[] { "Zombie", "Vampire", "Warewulf", "Blob" };
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
	  super.onCreate(savedInstanceState);

	  setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MONSTERS));

	  ListView lv = getListView();
	  lv.setTextFilterEnabled(true);

	  lv.setOnItemClickListener(new OnItemClickListener()
	  {
	    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	    {
	    	Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();
	    }
	  });
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>