Friday 2 March 2012

Simple Listview in Android

Listview is nothing but a A view that shows items in a vertically scrolling list.
The items come from the ListAdapter associated with this view.
Here we can see on example listview using setAdapter(ListAdapter adapter).
setAdapter(ListAdapter adapter) used to Sets the data behind this ListView.

Android Activity (SimpleListview.java)


package com.androidsurya.simplelistview;

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

public class SimpleListview extends Activity {

    private ListView mainListView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_listview);

        // Find the ListView resource.
        mainListView = (ListView) findViewById(R.id.mainListView);

        // Create and populate a List of planet names.
        String[] states = new String[] { "Andhra Pradesh",
                "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa",
                "Gujarat", "Haryana", "Himachal Pradesh" };
        mainListView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, states));


    }
  
}

UI Layout(activity_simple_listview.xml)

place this .xml file in layout folder

<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/mainListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

Register Activity in Android Manifest File


 <activity
            android:name="com.androidsurya.simplelistview.SimpleListview"
            android:label="@string/app_name" >

Ouput Screenshot:




No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...