14

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.

hasnain_ahmad
  • 325
  • 6
  • 17
Yaobin Then
  • 2,662
  • 1
  • 34
  • 54

3 Answers3

8

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:

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • I believe that this is the reason why **_id** is required for a cursor being used by an adapter. as per *The Cursor must include a column named "_id" or this class will not work.* at [CursorAdapter](https://developer.android.com/reference/android/widget/CursorAdapter.html). *The row id of the item that is selected* [AdapterView.OnItemSelectedListener](https://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html) could also be a little more informative. However, @Suragch, thanks for this detailed explanation, it clarified what I suspected. – MikeT Dec 16 '16 at 23:50
8

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.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • so does that mean the row id is assigned by the the view say list view and might not always be in order, and if I want to get the view clicked I use view.getchildat ( position) correct? – Yaobin Then May 01 '11 at 07:28
  • Hmmm, not quite. That's where things get tricky. getChildAt() comes from the ViewGroup class, so the index you pass to that is relative to the list's View, not the adapter. However, you ALREADY have the view that was clicked on (second parameter), so you shouldn't need to use getChildAt() for this purpose. As for the getItemId(), i'm not sure what the default behavior is for the Adapter you're using, but it might not have any useful meaning in your context. – dmon May 01 '11 at 13:18
  • Ahh, you're right, I should just use the view provided in the parameter and the position for other purposes. I once remembered that I used the ID to do something with some sqlite table's primary key _ID. Thanks alot! – Yaobin Then May 01 '11 at 14:34
1

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.

jonsca
  • 10,218
  • 26
  • 54
  • 62
prateek
  • 11
  • 1