Event Listener for a ListView in Single Selection mode

June 15, 2012
By

In this article we will create an event listener for listview in single selection mode ( Radio Button ) .


1. Create an Android project namely ListViewRadioListener

New Android Project

Figure 1: New Android Project


2. Select build target for this application

Select build target

Figure 2: Select build target


3. Enter application details

Application Details

Figure 3: Application Details


4. Open and update the file /res/layout/main.xml with the given below code


<?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/listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
/>
</LinearLayout>


5. Open and update the file /src/in/wptrafficanalyzer/listviewradiolistener/MainActivity.java


package in.wptrafficanalyzer.listviewradiolistener;

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

public class MainActivity extends Activity {

    // String array of countries for the listview
    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

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

        // Reference to listview object of the main layout
        ListView listView = (  ListView ) findViewById(R.id.listview);

        // Creating an array adapter for listView
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, countries);
        listView.setAdapter(adapter);

        // Defining the item click listener for listView
        OnItemClickListener itemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
                Toast.makeText(getBaseContext(), "You selected :" + countries[position], Toast.LENGTH_SHORT).show();
             }
        };

        // Setting the item click listener for listView
        listView.setOnItemClickListener(itemClickListener);
    }
}


 

6. Running the application

Application in execution

Figure 4: Application in execution


7. Download the project source code


8. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner