Saturday, 14 April 2012

ListViews

        Android ListView are probably amongst few of the most used View in Android. They are used to show data as a list of items.These data can have various flavour from the plane oL single line data to complex data with images and Multiple line of data and can even be interactive with use of buttons,checkboxes etc.

        ListView are represented as

<ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/YOUR ID  HERE" />
in layout xml

 android:id="@+id/YOUR ID HERE"
helps you to identify your listview from your class(Activity/Dialog/Fragment etc) with the string you get from "YOUR ID HERE"

for eg:.

        Consider an Activity containing a listview as show above layout xml, then it would look something like this in your code

pubic class MyActivity extends Acitity{
    ListView mListVew;
    // some code here
    //You can initialize it as follows
    //.......
    mListView=(ListView) findViewById(R.id."YOUR ID HERE");

        now we have an instance of listview and ready to fill it with data that we want.
    The process of filling the ListView is called inflating and "Adapters" are used to do these.Adapters basically decide how and what kind of data can a list view display.Adapters are attached to ListView  as follows,

mListView.setAdapter("Name of your adapter");

        These adpater must be child of BaseAdapter or its type.Customizing these adapters gives the magic that you want your ListView to have.

1 comment:

  1. How to create a ListView with collapsible items. A post on that will be helpful.

    ReplyDelete