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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ListView Example" />

    <ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="42dp" >
    </ListView>

    </RelativeLayout>
  • 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"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    @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;
    }

    }
  • Here we have used some packages which are as follows:
  • android-listview

  • 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:
  • android-listview

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

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

  • 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:
  • android-listview