It is pretty simple. The second parameter of the onItemClick is a View that contains just the item that was clicked. (Or tapped, whatever.) If you used Eclipse to automagically generate the callback method for you, the variable will be named "arg1", which is what we will assume for this example.
Inside of the onItemClick method, you can figure out the text using the following code :
String s = ((TextView)arg1.findViewById(android.R.id.text1)).getText().toString();
Pretty simple, huh? But, the code doesn't quite explain itself. So, I'll take a stab at it.
A ListView is exactly what the name says. It is a list of views. When you create the ArrayAdapter
Inside the layout is a single definition. It is a TextView named "text1". Since it is a built-in layout, we will find the layout information under "android.R", instead of just "R" where our layout definitions show up.
So, by executing findViewById() on the view that was clicked, and passed in as "arg1", we can get the text that the user clicked on.
Pretty simple huh?
man this is what i was looking for 2 days :) thx
ReplyDeleteThank you so much!!!!!
ReplyDeleteit work for me with view instead of arg1
ReplyDeleteString fraccionValues = ((TextView)view.findViewById(android.R.id.text1)).getText().toString();
You are the BEST kind of human being. This is what I needed to figure out to finish my app project for school.
ReplyDeleteMuch love.