Android – ListView – 1 – Simple

By -

Based on the requests I have received for writing articles on ListView, I have decided to write a series of article for the beginner developers and this is the First part about simple listview implementation in Android. This series will have articles from simple ListView to advanced listview.

Purpose: ListView is used to display list of choices for user to select.

For example:
listitem_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView
    	android:layout_height="wrap_content"
    	android:id="@+id/listView1"
    	android:layout_width="match_parent">
    </ListView>
</LinearLayout>

ListView1Activity.java

package com.technotalkative.listview1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListView1Activity extends Activity implements OnItemClickListener
{
    /** Called when the activity is first created. */

	ListView lview;
	private final static String month[] = {"January","February","March","April","May",
		"June","July","August","September","October","November","December"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        lview = (ListView) findViewById(R.id.listView1);
        lview.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,month));
        lview.setOnItemClickListener(this);
    }

	public void onItemClick(AdapterView arg0, View arg1, int position, long id) {
		// TODO Auto-generated method stub
		Toast.makeText(this,"Item clicked => "+month[position], Toast.LENGTH_SHORT).show();
	}
}
Paresh Mayani

Lazy android developer, exploring the horizon of android development since 7 years, currently working as a Technical Lead - Android at Simform Solutions, India. Former Application Architect at KarConnect. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...