onItemClick ... passing the position in my array to somewhere else
Does the "position
" parameter in the onItemClick
reference the position in the Adapter
, which might include, say, the footer and header as well. I'm asking bec开发者_运维知识库ause I want to be able to pass the actual position in the underlying array that I'm using with my adapter
. Can I just pass "position
" and I'll be good to go?
If I remember correctly the position is the position in your items array. Header and footer items I do not think get included, because your overridden method getPosition() simple returns the position you are seeking within the array of items.
In answer to your question, yes, headers and footers are included in the position
parameter.
The documentation for onItemClick states the parameters are as follows:
parent The AdapterView where the click happened.
view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position The position of the view in the adapter.
id The row id of the item that was clicked.
So if you're looking to fetch an object from your underlying array, you should use the long id
parameter instead, which returns the underlying array index:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Get the item that was clicked
Object objectToUse = myAdapter.getItem( (int) id );
}
精彩评论