Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all :)

I have a listview, and I want to get the selected item index to do some process on it

how can I get this index as an integer value ?

this is my code:

Java
final ListView list = (ListView)findViewById(android.R.id.list);
		list.setClickable(true);
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
	    
		  }
		});


thanks all :)
Posted

Java
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {	    
		  }


The int position will give you the selected item index in int.
 
Share this answer
 
v2
Comments
TorstenH. 9-Mar-12 1:57am    
that is the position of the View in the Adapter, not the selection index.
Jaydeep Jadav 9-Mar-12 2:40am    
this is the position of selected item on listview as we select the 5th item in listview the index is 5 and we get 5 in our position argument of onItemClick method.
[no name] 10-Mar-12 21:14pm    
Ok I tried it before, but it return null

is there any error with my code above ?
i user like below and working nice

Java
mylistview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				MyObject selection = (MyObject) arg0.getItemAtPosition(arg2);
				
			}
		});


the different is
i havent use
Java
list.setClickable(true);

and put
Java
@Override
attribute
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900