29

I'm confused about the difference between postion and row id in ListActivity.onListItemClick().

The documentation has the following:

position  The position of the view in the list
id        The row id of the item that was clicked

Is there any practical difference between the two? I assume, since there are two different parameters, that there are situations where the values are different, but based on the wording used in the documentation, it's hard to see what that difference may be. Is it that one of them can hold the value of a database row ID provided by a Cursor or some other special capability?

Paul Holden
  • 850
  • 1
  • 8
  • 24
  • 2
    Say you sorted the list. Then the position would be very different than the id. Position is top to bottom and id is a unique identifier. – nathan hayfield Oct 19 '12 at 00:56
  • How is the unique identifier assigned then? Is the order of items in the ListView when it is first populated, before any operation like sorting is done to it? – Paul Holden Oct 19 '12 at 01:27

2 Answers2

34

position: The position of the view in the list
id: The row id of the item that was clicked

A position starts from 0, top to bottom of the ListView, whereas to get a proper row id it is important that the cursor, which was passed to the adapter, contains a column called '_id' representing unique id for each row in the table.

If you are using an ArrayAdapter, position and id become the same. The id will be the returned value of Adapter.getItemId(int) so if you use an ArrayAdapter it will be the same as the position - a quick search of the ArrayAdapter source code shows it's using return position; to work out the id.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • If the table doesn't contain a `_id` field, then is it safe to say that position and row ID will be the same? – Paul Holden Oct 19 '12 at 01:27
  • 5
    Seems that if you are using an arrayAdapter it is the same. The id will be the returned value of Adapter.getItemId(int) so if you use an ArrayAdapter it will be the same as the position - a quick search of the ArrayAdapter source code shows it's using return position; to work out the id :) – Lazy Ninja Oct 19 '12 at 02:10
  • 3
    This overlooks the case of having a `ListView` with a header view. In that case position = id + 1. [See my answer](http://stackoverflow.com/a/24531354/383414) – Richard Le Mesurier Jul 02 '14 at 12:43
28

The answer by Lazy Ninja is mostly correct, however overlooks the case of using an ArrayAdapter with a header view added by calling the ListView.addHeaderView(View) method.

If your list has a header item, it will be at position 0.

So the first "real" item in the list will have:

  • position = 1
  • id = 0

In general, position = id + number_of_header_views


This is important to realise if you decide to add a header view to a list in code that is already working correctly.

If you are accessing your items using position, then your code needs to be updated along the following lines:

  • use position - 1 as the index
  • use id as the index
  • use ListView.getItemAtPosition(int) instead of Adapter.getItem(int)

Also see this question for related discussion:

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
  • 2
    The difference posted by you is very critical point in understanding the listview with header.. Thank you a lot for your additional answer here.. – Kushal May 12 '15 at 06:21