Wednesday, December 14, 2011

Android: ListView Tutorials

To create List View, in xml just drag the ListView widget in layout. You can use DroidDraw for creating the layout. For creating the ListView extends Activity and create object of class ListView.

Let the code file name is ListViewExample.java.


package com.ex;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LWActivity extends Activity
{
    ListView lw;
    String[] dataSet={"android","ios","bada","BlackBerry OS","Windows","Symbian"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lw=(ListView)findViewById(R.id.listView1);
        ArrayAdapter myAdapter=new ArrayAdapter (this,android.R.layout.simple_expandable_list_item_1,dataSet);
        lw.setAdapter(myAdapter);
    }
}

For main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:id="@+id/listView1"
android:layout_width="318px"
android:layout_height="49px"
>
</ListView>
</LinearLayout>



Here, Adapter is used to get the data for List View. We have made ArrayAdapter here.
Use


No comments:

Post a Comment