Android - ListView Example


ListView is a view group that displays a list of scrollable items. The display of items in a list is very common in mobile application.

Building layout with an Adapter and a ListView

We create an adapter that fetch content from an array or database query. Then adapter converts each item of array into a view.  After that the view is dynamically inserted into a list.

 

How to implement listview

  1. create a layout of listview.
  2. create a layout for view-item to place inside listview.
  3. Create an adapter to hold our array data.

Create a xml file named activity_main.xml and add the following line of code inside it.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

This activity_main.xml will be our listview. Now we want to add items in our listview.

So create one more xml file and name it list_view_row.xml. In this file we will add one textview which will be plugged inside listview as a view-item.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/listText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

In the above code we have made the text appearance of TextView to be large. Now let us move towards implementing activity.

Steps involved in implementing activity for listview in android.

  1. Create reference of ListView and one String array.
  2. In setContentView() pass the listview layout i.e R.layout.activity_main.
  3. Fill your string array which is holding the item to be placed inside listview.
  4. Create an array adapter and pass list_view_row xml layout, a textview id R.id.listText and a string array holding the items.
  5. Finally set adapter inside listview.
package com.example.listview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	ListView listview;
	String[] foody;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listview = (ListView)findViewById(R.id.listView);
        // Array holding our data
	String[] foody = {"pizza", "burger", "chocolate", "ice-cream", "banana", "apple"}; 
	//adapter which will convert each data item into view item.
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_view_row, R.id.listText, foody);
	//place each view-item inside listview by setting adapter for our listview
        listview.setAdapter(adapter);
		}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

In the code below this refers to our own activity. It is passed as a context which represents our own activity. So that the adapter knows which activity is using it.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_view_row, R.id.listText, foody);
	


Finally, listview.setAdapter() helps to place view-items inside the listview.

ScreenShot of listview in android.

In the next tutorial, we will study about event handling in listview. We will make each item of our listview clickable.