• Have Any Question?
  • contact@eitworld.com

ListView in Android

    • Android provides a list view which contains the several options where we can select one or multiple options from the given list.
    • In android, list can be scrollable means when the items are more than the list view then the scrolling; used to display the other list items which are not displayed on the list view.
    • In android, adaptor is used to provide the view of list items to the list view means list view gets the data to show with the help of adaptor.
    • In android, there are two types of adaptor such as ArrayAdaptor and CursorAdapotor.
    • ArrayAdaptor is used to handle data from the array but CursorAdaptor is used to handle the data from the database.
    • We can develop our own adaptor class just by extending the BaseAdaptor class.
    • We can directly use the ListView in layout as the user interface component.
    • When we use the list view in our application then we have to use the ListAdaptor which is used to manage the data. This adaptor must be written on the onCreate() method of our activity using the setListAdaptor() method.
    • When we select any item from the list then onListItemClick() method is called.
    • Below is the demo example of the ListView:
    • The xml layout file is as follows:

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
tools:context=”.MainActivity” >

android:text=”ListView Example” />

android:layout_marginBottom=”42dp” >


    • Here we have used the relative layout and we have used the tag to define the list view in the xml layout file. Here we have used some attributes to define the width, height, margin of the list view, and we have used a text view to display the message.
    • The java file is as follows:
package com.example.listviewdemo;

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 lv;
String[] countNum = new String[] {“One”,”Two”,”Three”,”Four”,”Five”};

protected void onCreate(Bundle savedInstanceState) {

lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter adp = new ArrayAdapter(this,android.R.layout.simple_list_item_1,countNum);

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;


    • Here we have used some packages which are as follows:


    • Here android.widget.ListView provides the methods related to the list by which we can use the list in the entire layout. After that we have to declare the ListView type variable so that we can use the list view which is defined in the xml layout file as follows:


    • Now we have to declare a string array that contains some values named countNum as follows:


    • Now we have to register the list view inside the onCreate() method as follows:


    • Here we have registered the list view using the id which we have provided in the xml layout file. Here we have created an ArrayAdapter object that is of the string type; takes the three parameters; first parameter is the context means current activity or current context, second parameter is the type of the layout of the list view, it is default provided by the android system, and the last parameter is the array name which we have created, here just pass the name of the array.
    • After that we have to call the setAdapter() method using the list view object and this method takes one parameter that is object of the ArrayAdapter means “adp” in this example.
    • The output is as follows:


Real Time Web Analytics