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:
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:
package com.example.cl5_instructor.hellolistview; import android.app.ListActivity; import android.support.v7.app.AppCompatActivity; 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;
These will be needed by the program lines that is accessing the methods that belongs to the above components.