Monday 6 August 2012

Learning about most effective View in Android 'ListView'


Hello every one, in this post i would like to share about the most powerful view in our android that is ListView (i felt  this view is the most effective one in Android). ListView class which is capable of displaying a scrollable list of items. The items can be of any type.

We have lots of ways to set values into a ListView. Some ways that i learned are 
  1. Setting ListView with layout(view) provided by Android resources.
  2. Setting ListView with our own layout.
  3. Setting ListView with different images as item.

Now i am gonna explain each way one by one..

Setting ListView with layout provided by Android resources

Add Listview to main.xml

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="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Then create an array named 'values' in the corresponding acitvity and put some items in it. Create Listview object and initialize it with corresponding id. Then comes the magic thing ‘ArrayAdapter’
As we know Listview is a group of items, each item will contain a TextView. Now the job of  ArrayAdapter is to take each values from the array ‘values’ and sets the TextView with that value, then this TextView is set as an item for ListView. So the ArrayAdapter works or loops as the length of the array we used. On the initialization of ArrayAdapter what we want to do is, indicate which array we are using and  which TextView (or view) we want to use as item of ListView.

ListExample Activity for main.xml

public class ListExample extends Activity {
       ListView lv;
       String[] values;
       ArrayAdapter<String>myAdapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        lv.setAdapter(myAdapter);
    }
       public void init() {
              // TODO Auto-generated method stub
              lv=(ListView)findViewById(R.id.listView1);
              values=new String[]{"folder1","image1.png","text.txt","image2.png","folder2","text2.txt"};
              myAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, values);
       }
}

Here we have used android.R.layout.simple_list_item_1. The simple_list_item_1 will contain a TextView. To see the content in simple_list_item_1 navigate to android-sdk-windows/platforms/android-(api level)/data/res/layout. Here we see a lots of xml file which provided by Android resources

simple_list_item_1.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
/>

The output for the above code will be like this
That's all for this post. The next post will explain 'Setting Listview with our own layout'.

2 comments: