Search code examples
javaandroidlistviewandroid-listfragmentlistadapter

When I add a header in a listview I need to do position - 1 to access the backend list


I have a ListFragment and a custom ArrayAdapter.
I do:

getListView().addHeaderView(inflatedHeader, null, false);
CustomAdapter adapter = new CustomAdapter(dataArrayList);
setListAdapter(adapter); 

It works but I noticed that in the getView method of the adapter it is called with position starting from 0, while the onListItemClick item when I click on an element of a list I need to do position - 1 to get the corresponding item from the dataArrayList I passed in.
So basically I click on the first element of the list after the header and I get in the onListItemClick position == 1, and therefore I need to do getListAdapter().getItem(position - 1) in the method.
Is this how it is supposed to work or am I doing something wrong?


Solution

  • you should use getItemAtPosition(int position) instead of getItem(int position) in your item click listener, which takes already in consideration headers and footers view