4

I´ve made quite many listeners and never really thought of it until now.

Basic Listview listener:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(), "you clicked on: " +  position + " id: " + id, Toast.LENGTH_SHORT).show();

        }
    });

According to documentary:

position is: The position of the view in the adapter.

id is: The row id of the item that was clicked.

Could someone good-hearted explain the difference for me.

Thx in advance..

Towfik Alrazihi
  • 536
  • 3
  • 8
  • 25
Sindri Þór
  • 2,887
  • 3
  • 26
  • 32

1 Answers1

3

According to the docs

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

The position is the position of the view inside the parent. For your case (a ListView) it means the index of the row. (Starting at 0) For ArrayAdapter and SimpleAdapter id is the same as position. For a CursorAdapter, id is the row id of the table.

Related Answers:
Practical Difference Between position and row id in onListItemClick() https://stackoverflow.com/a/25622142/2278598

Community
  • 1
  • 1
Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55