Android ListView
The traditional listbox widget in Android is known as ListView. We really need to invoke setAdapter() to provide data and child views to the ListView as well as have to connect a listener by making use of setOnItemSelectedListener() to understand when the selection has changed. With that, you now have a fully functioning listbox.In case your activity is actually containing just a single list, then you might very well think of creating your activity as a subclass of ListActivity, as opposed to the regular Activity base class.
If your main view is simply the list, you don’t actually really need to provide a layout — ListActivity will certainly build a full-screen list for you.
If you would like to personalize the layout, put the id of the ListView as @android: id/list, so ListActivity understands which widget is the main list for the activity.
Lets create a brand new android project to experiment with listview, I named my project as AndroidListView. As well as created an activity called ListViewActivity and ensure we have to extend ListActivity as this gonna consist of just a single list.
Creating Layout for ListView
main.xml
<?xml version="1.0" encoding="utf-8"?>
<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/user_selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:choiceMode="singleChoice"/>
</LinearLayout>
Java code to configure the list
ListViewActivity.java
package com.vimaltuts.android.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewActivity extends ListActivity {
/** Called when the activity is first created. */
private TextView userSelection;
private static final String[] items={"Android","Bluetooth","Chrome","Docs","Email",
"Facebook","Google","Hungary","Iphone","Korea","Machintosh",
"Nokia","Orkut","Picasa","Singapore","Turkey","Windows","Youtube"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,items));
userSelection=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent,View v,int pos,long id){
userSelection.setText(items[pos]);
}
}


With ListActity, we can set the list adapter by using setListAdapter() in this example, providing an ArrayAdapter wrapping an array of strings. Then we need to override onListItemClick(), to determine as soon as the list selection changes, and take appropriate steps based on the supplied child view and position(Here, we updating the label with the text for that position).
The second parameter to our ArrayAdapter is android. R. layout. simple_list_item_1 which controls the look of the row.
List of Layout Constants
- activity_list_item
- browser_link_context_header
- expandable_list_content
- list_content
- preference_category
- select_dialog_item
- select_dialog_multichoice
- select_dialog_singlechoice
- simple_dropdown_item_1line
- simple_expandable_list_item_1
- simple_expandable_list_item_2
- simple_gallery_item
- simple_list_item_1
- simple_list_item_2
- simple_list_item_activated_1
- simple_list_item_activated_2
- simple_list_item_checked
- simple_list_item_multiple_choice
- simple_list_item_single_choice
- simple_selectable_list_item
- simple_spinner_dropdown_item
- simple_spinner_item
- test_list_item
- two_line_list_item
Android List View — Multiple Selection
There may be occasions when you will want list that tracks a user’s selection, or perhaps multiple selections. ListView will be able to handle that too, however it requires a couple of further changes.
Very first, you will have to call setChoiceMode() by providing either CHOICE_MODE_SINGLE for single selection or CHOICE_MODE_MULTIPLE for multiple selection as the value. By using the getListView() You can get your ListView through ListActivity.
You may also declare this via the android: choiceMode attribute in your layout XML.
Then you will have to make use of either android.R.layout.simple_list_item_single_choice for single-choice or android.R.layout.simple_list_item_multiple_choice for multiple-choice lists, correspondingly in your ArrayAdapter.
Creating Layout for ListView
main.xml
<?xml version="1.0" encoding="utf-8"?>
<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/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:choiceMode="multipleChoice"/>
</LinearLayout>
Java code to configure the list
ListViewActivity.java
package com.vimaltuts.android.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewActivity extends ListActivity {
/** Called when the activity is first created. */
private TextView selection;
String str="";
private static final String[] items={"Android","Bluetooth","Chrome","Docs","Email",
"Facebook","Google","Hungary","Iphone","Korea","Machintosh",
"Nokia","Orkut","Picasa","Singapore","Turkey","Windows","Youtube"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice,items));
selection=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent,View v,int position,long id){
str+=" "+items[position];
selection.setText(str);
}
}
getCheckedItemPositions() — To find out which items the user checked on our ListView,
setItemChecked() — To check (or un-check) a specific entry ourselves.
VimalTuts Code Junction 

Nice tutorial and easy to follow, I have a doubt, is it possible to implement 3 listviews int the same activity? The acticity would be TextView – ListView (with a couple of options) – TextView – ListView (with a couple of options) – TextView – ListView (with a couple of options). If yes, could you tell me how to do it?
thanks in advance
I’m getting an error exception, while running this program.