Pages

Wednesday, May 19, 2010

Android Listview Example


In Android, Listview is used to show a list of items in a vertically scrolling list.
For instance, in a Registration form when we are selecting professions a list of items will be displayed. We can use Listview to display the list of items.
Your XML file should look like
*********************************************
xml version="1.0" encoding="utf-8"?>
2<LinearLayout android:id="@+id/LinearLayout01"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
6 <ListView android:id="@+id/ListView01"
7 android:layout_width="wrap_content"
8 android:layout_height="wrap_content" />
9LinearLayout>
Your Java code looks like
01public class ListviewExample extends Activity
02{
03private ListView lv1;
04private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
05@Override
06public void onCreate(Bundle icicle)
07{
08super.onCreate(icicle);
09setContentView(R.layout.main);
10lv1=(ListView)findViewById(R.id.ListView01);
11// By using setAdpater method in listview we an add string array in list.
12lv1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1 , lv_arr));
13}
14}



The output will look like
listviewexample

No comments:

Post a Comment