Monday 13 June 2011

Android- Simple ListView Tutorial -1


List View -: is one of Android's most widely used widgets. It is rather easy to use, very flexible, and incredibly powerful. "ListView"  is used to show a list of items in a vertically scrolling list. "ListView" gets the data to display via an adapter.
Adapter -:Android has two standard adapters, ArrayAdapter and CursorAdapter . "ArrayAdapter" can handle data based on Arrays or Lists while "SimpleCursorAdapter" handle database related data.
                                                     You can develop your own Adapter by extending these classes or the BaseAdapter class. An adapter which must extend "BaseAdapter" and is responsible for providing the data model for the list and for converting the data into the fields of the list.
ListActivity -: An activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.
Now we are going to see about a simple listview example. In Android


Step 1-: Create a simplelistlayout.xml file in side the layout folder of the project 
<?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>

</LinearLayout>

Step 2-: Create a ListViewExample class .
public class ListViewExample extends Activity {
    /** Array of items they are display in ListView */
private String Items_arr[]={"List View Item-1","List View Item-2","List View Item-3","List View Item-4","List View Item-5","List View Item-6","List View Item-7","List View Item-8","List View Item-9"};
    private ListView ListView01;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
        ListView01 =(ListView)findViewById(R.id.ListView01);
        /** By using setAdpater method in listview we an add string array in list.*/
        ListView01.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , Items_arr));
    }
}

Step 3-: Run the application.



No comments:

Post a Comment