In this article we will create an event listener for listview in single selection mode ( Radio Button ) .
1. Create an Android project namely ListViewRadioListener
2. Select build target for this application
3. Enter 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
7. Download the project source code

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

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