How to Create a Simple ListView in Android

The display of elements in a lists is a very common pattern in mobile applications. The user sees a list of items and can scroll through them. If he selects one item, this can update the ActionBar or triggers a detailed screen for the selection.
Android provides the ListView class which is capable of displaying a scrollable list of items. These items can be of any type(Array Of anytype).

For example the following listing shows a layout file with includes a ListView.
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”;
android:orientation=”vertical” >
android:layout_height=”wrap_content” >

Adapters and ListView implementations

A ListView receives its data via an adapter. The adapter also defines how each row in the ListView is displayed.
The adapter is assigned to the list via the setAdapter method on the ListView object.
An adapter extend the BaseAdapter class. Android provides several standard adapters; Like ArrayAdapter and CursorAdapter.


ArrayAdapter can handle data based on Arrays or java.util.List.The ArrayAdapter class can handle any Java object as input. It maps the data of this input to a TextView in the layout. You can define one in the constructor otherwise the ID will be used.
ArrayAdapter uses the toString() method of the data input object to determine the String which should be displayed.

   [Except these two another adapter is SimpleCursorAdapter,which can handle database related data. (Check other post)]

The Android platform provides default layouts for rows in ListViews. For example
android.R.layout.simple_list_item1. By default ArrayAdapter uses the ID, but you can define another one in the ArrayAdapter constructor.

The following coding shows an example of the usage of the ArrayAdapter class and a predefinedlayout from the Android system.

Let’s get start by creating a project in Android ADT.

1. Create a new project by going to File ⇒ New Android Application. Fill all the details  As Application Name:SimpleListView,Project Name:SimpleListView,Package Name:com.test.simplelistview  and click on Next button to finish .

2 .Once the project is created open your activity_main.xml (res/layout) and paste the ListView (Shown above) code.

3.Open class and paste following code.

import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

ListView listView = (ListView) findViewById(;
String[] values = new String[] {“sunday”,”monaday”,”tuesday”,”wednesday”,”thrusday”,”friday”,”saturday”};
// First paramenter – Context
// Second parameter – Layout for the row
// Third parameter – ID of the TextView to which the data is written
// Forth – the Array of data
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,  values);
// Assign adapter to ListView

listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String filename = adapter.getItem(position).toString();

 // ListView Clicked item value
//   String  filename= (String) listView.getItemAtPosition(position);
//   remove final from ArrayAdapter to use.

Toast.makeText(getApplicationContext(), “Slected File=” + filename+”;”+ ” Selected Position=”+position, Toast.LENGTH_LONG).show();



4. The final step is to  AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”;
android:versionName=”1.0″ >

android:targetSdkVersion=”16″ />

android:theme=”@style/AppTheme” >
android:label=”@string/app_name” >
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
<activity android:name=”com.testview.listviewtest.SundayActivity”></activity>
<activity android:name=”.MondayActivity”></activity>


6. Finally run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s