ListView in Android :

In a Android, ListView is arranges components or items  in a vertical scrollable list. If you have a long list of items that you simply want to show to the user, the ListView view is the answer. This example demonstrates the way to use the ListView to show a listing of items contained within an array.

android listview example

Note : This example (Project) is developed in Eclipse Version Kepler, tested on Android 4.4.2 (KitKat Version) and SDK build-tools 22.6.3

Solution Stuff :

In Android, use the ListView, use the <ListView> element and put it into your UI file and use “android.widget.ListView” class to render a ListView.

Complete Code :

1. Open “res/layout/activity_main.xml” file. To use the “ListView ” element and put it into your UI file, such as the activity_main.xml file:

File : res/layout/activity_main.xml

2. Open “MainAcitvity.class” file and add following JAVA code.

In the activity, extend the ListActivity base class and use the setListAdapter() method to bind the ListView to an ArrayAdapter object (which in turn loads the string array). You specify the type of ListView you want by specifying the android.R.layout.simple_list_item_1 constant, which is able to simply show a listing of items inside the ListView:

File : src/packagename/MainActivity.class

The onListItemClick() method is that the callback method used once the user has selected  an item inside the ListView. The second argument indicates the index of the items inside the ListView that was selected . using this information, you’ll reference it against your array and obtain the name of the item selected .

Figure shows what the ListView looks in Android emulator.

android listview example1

Download Android ListView example Source code :

  1. Hi,where to put text of each of this category list,this is category and where to put text inside of each this category list as a content of this list please help me

Leave a Reply