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 t开发者_开发百科hat 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:
- https://stackoverflow.com/a/25622142/3681880
- https://stackoverflow.com/a/9863279/3681880
- https://stackoverflow.com/a/12966006/3681880
- https://stackoverflow.com/a/24531354/3681880
Position will return all the names or the values placed on that position, e.g., if you are displaying raj,kamal,prateek
in a list and position 2 gets selected it will display(return) kamal
.
If you will go for row ID, it will return particular fixed numbers or IDs located to that item, like 1,2,3,4 ...
on same program if you will select row ID it will return 2, and for position it will return kamal
.
精彩评论