Wednesday, March 21, 2012

Simple ListView Example

In this post I show how to create a Simple ListView. Create a xml file listview_row.xml which contains a TextView for displaying the values in ListView.

listview_row.xml

<?xml version="1.0" encoding="utf-8" ?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp">
</TextView>

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" >



    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        </ListView>


</LinearLayout>

SimpleListViewActivity.java

public class SimpleListViewActivity extends Activity
{
 private ListView listview;
private ArrayAdapter<String> listAdapter;

@Overridepublic void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);

listView=(ListView)findViewById(R.id.listView1);

String[] actress=new String[]{"Anushka","Bhavana","Charmy","Deepika","Genelia","Hansika","Kajal Agarwal","Nayanatara","Namitha"};


ArrayList<String> actressList=new ArrayList<String >();
actressList.addAll(Arrays.asList(actress));


listAdapter=new ArrayAdapter<String>(this,R.layout.listview_row,actressList);
listView.setAdapter(listAdapter);


}
}


Now Run the program.

 

No comments:

Post a Comment