ListView: Sample Program 1

This example shows a ListView displaying a list of countries. We start by creating a new Android project and defining our layout file that will be used in the sample program.

Layout XML File

This is created by first creating an xml for layout file named list_country.xml. The xml file just contains the generic layout for our list. The list_country.xml file should be saved in /res/layout/ under the current project folders. Below is the contents of the list_country.xml file:

Source Code

Below is the source code of the program. We name the program as HelloListViewActivity.java.

In the program, make sure you import the following components:

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

These will be needed by the program lines that is accessing the methods that belongs to the above components.

 

HelloListView Output

Be the first to comment

Leave a Reply

Your email address will not be published.


*