By Alvin Alexander. Last updated: July 17, 2016
When you need to determine which item in a ListFragment
has been tapped/selected in an Android application, this code shows how to do that:
@Override public void onListItemClick(ListView listView, View view, int position, long id) { Tweet tweet = (Tweet)(getListAdapter()).getItem(position); Log.d(TAG, tweet.getText() + " was clicked"); Toast.makeText(getActivity(), "Click!", Toast.LENGTH_SHORT).show(); }
The highlighted code shows how I determine which “Tweet” in a list of tweets a user has selected. (The last two lines in the method just show what I currently do with that tweet after I get it.)
The key in this solution is knowing to call the getItem
method on the getListAdapter
method, using the position
parameter that is passed into the onListItemClick
method.
If you needed to see how to determine which item in an Android ListFragment
was selected by the user, I hope this is helpful.