I've got confuse on the 3rd and 4th parameter of onItemLongClick(...). According to AdapterView.OnItemLongClickListener
position - The position of the view in the list
id - The row id of the item that was clicked
I couldn't make any sense out from these, advice please.
position
is the clicked element's position in your Adapter (so you can do adapter.getItem(position)
)
row id
is the id that corresponds to that element, what your Adapter returns in the getItemId()
method.
The position is the position of the view in the parent. For a ListView
, it is the row number. The top row is position 0, the second row is position 1, the third row is position 2, etc. Note that if your ListView
has a header view (like if you did ListView.addHeaderView(View)
) then the header view would be position 0 and the actual rows would start their numbering at 1.
Sometimes id is the same as position and sometimes it is different. If you are using an ArrayAdapter
or SimpleAdapter
then they are the same (except in the case that there is a header view and then they are off by one). For a CursorAdapter
(and consequently a SimpleCursorAdapter
) the id returns the row id of the table, that is, _id
. Position is a long rather than an int because a database could theoretically have more rows than an int could hold whereas a ListView
wouldn't.
Here are a few other related answers:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With