Get Selected Item In ListView Android
Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create a Blank Activity and click on Finish button.
Step 2 : Open res -> layout ->activity_main.xml and add following code :
< RelativeLayout xmlns : android = “http://schemas.android.com/apk/res/android"
xmlns : tools = “http://schemas.android.com/tools"
android : layout_width = “match_parent”
android : layout_height = “match_parent”
android : paddingBottom = “@dimen/activity_vertical_margin”
android : paddingLeft = “@dimen/activity_horizontal_margin”
android : paddingRight = “@dimen/activity_horizontal_margin”
android : paddingTop = “@dimen/activity_vertical_margin”
tools : context =”com.saud.listview_listactivity.MainActivity” >
< ListView
android : id = “@+id/list”
android : layout_width = “match_parent”
android : layout_height = “wrap_content”
android : layout_below = “@+id/textview1” >
< / ListView >
< / RelativeLayout >
Step 3 : Open src -> package ->MainActivity.java and add following code :
package com . saud. android .listview_getselecteditem ;
import android . app . Activity ;
import android . os . Bundle ;
import android . view . View ;
import android . widget . AdapterView ;
import android . widget . AdapterView .OnItemClickListener ;
import android . widget . ArrayAdapter ;
import android . widget . ListView ;
import android . widget . TextView ;
import android . widget . Toast ;
public class MainActivity extends Activity {
static final String [ ] FRUITS = new String [ ] {“Apple” , “Banana” , “Coconut” ,
“Durian” , “Guava” , “Kiwifruit” , “Jackfruit” ,”Mango” , “Orange” ,
“Pear” } ;
@Override
public void onCreate ( Bundle savedInstanceState ){
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_main ) ;
ListView listView = ( ListView ) findViewById( R . id . list ) ;
ArrayAdapter <String> adapter = newArrayAdapter <String> ( this ,
android . R . layout . simple_list_item_1 ,FRUITS ) ;
listView . setAdapter ( adapter ) ;
listView . setOnItemClickListener ( newOnItemClickListener ( ) {
public void onItemClick ( AdapterView < ? >parent , View view ,
int position , long id ) {
// When clicked, show a toast with the TextView text
Toast . makeText ( getApplicationContext ( ) ,
( ( TextView ) view ) . getText ( ) , Toast .LENGTH_SHORT ) . show ( ) ;
//String selectedItem = parent.getItemAtPosition(position).toString(); or
//String selectedItem = ((TextView) view).getText(); //for simple_list_item_1 layout or
//String selectedItem = (TextView) view.findViewById(android.R.id.text1); //for simple_list_item_1 layout with textview id or
//String selectedItem = FRUITS[position]; // using selected position
}
} ) ;
}
}
Step 4 : Open AndroidManifest.xml and add following code :
<? xml version = “1.0” encoding = “utf-8” ?>
< manifest xmlns : android = “http://schemas.android.com/apk/res/android"
package =”com.saud.android.listview_getselecteditem”
android : versionCode = “1”
android : versionName = “1.0” >
< uses — sdk
android : minSdkVersion = “16”
android : targetSdkVersion = “19” / >
< application
android : allowBackup = “true”
android : icon = “@drawable/ic_launcher”
android : label = “@string/app_name”
android : theme = “@style/AppTheme” >
< activity
android : name =”com.skholingua.android.listview_getselecteditem.MainActivity”
android : label = “@string/app_name” >
< intent — filter >
< action android : name = “android.intent.action.MAIN”/ >
< category android : name =”android.intent.category.LAUNCHER” / >
< / intent — filter >
< / activity >
< / application >
< / manifest >