Saturday 1 October 2011

Android Listview


Today, we are going to see about a simple listview example. In Android, Listview is used to show a list of items in a vertically scrolling list.  Learn a listview of android array in this tutorial.
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
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
[/sourcecode]
Your Java code looks like
[sourcecode language="java"]
public class ListviewExample extends Activity
{
private ListView lv1;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
}
}
The output will look like
listviewexample

No comments:

Post a Comment