Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between RowId and Position in onItemLongClick's parameter

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.

like image 515
Yaobin Then Avatar asked Apr 30 '11 18:04

Yaobin Then


2 Answers

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.

like image 195
dmon Avatar answered Nov 07 '22 13:11

dmon


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:

  • https://stackoverflow.com/a/25622142/3681880
  • https://stackoverflow.com/a/9863279/3681880
  • https://stackoverflow.com/a/12966006/3681880
  • https://stackoverflow.com/a/24531354/3681880
like image 8
Suragch Avatar answered Nov 07 '22 13:11

Suragch