I have a onListItemClick that returns the text from item in the listview with:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
TextView tv = (TextView)findViewById(R.id.item_title);
Toast.makeText(getApplicationContext(), (tv).getText(),
Toast.LENGTH_SHORT).show();
}
The problem is the data it returns is from the item at the top of the current listview no the actual list item that is clicked. To illustrate if I had a list that was like this
A
----- (Start of Viewable area)
B
C
D
------(End of viewable area)
E
If I click on item D the toast will return the Title for item B. Any ideas how I can fix this?
That method is being called from within your custom ListView, correct? From what I can tell, “findViewById” is searching the entire ListView for a view with the id “R.id.item_title”. Since you have multiple list items on the screen, it’s pulling the top one. Instead, call
so that it searches child views of the View object passed as parameter “v”.