androidspinneronitemselectedlistener

Why Android Spinner onItemSelectedListener have two identical params


Can anyone please clarify why onItemSelected() method has both position and id params?

First I read the docs and didn't understand the difference. Then I tried watching both parameters in debug and they always have exactly same values. Also I wasn't able to find answer in google for that question, so my last resort is stackoverflow.

What's the difference between position and id?

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

Solution

  • These two parameters may have different values depending on how the underlying data is stored. For an ArrayAdapter, they are likely the same. However, for a CursorAdapter, the position is the position within the Spinner dropdown and the id is the primary key from the database table.