how - widget listview android

ListView addHeaderView causes position to increase by one? (8)

Below is a code snippet with a ListView. I added an emptyView and a headerView. Adding the headerView causes the position in the onItemClick to be increased by one.

So without the headerView the first list element would have position 0, with the headerView the position of the first list element would be 1!

This causes errors in my adapter, e.g. when calling getItem() and using some other methods, see below. Strange thing: In the getView() method of the adapter the first list element is requested with position 0 even if the headerView is added!!

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ListView list = (ListView) viewSwitcher.findViewById(;
    View emptyView = viewSwitcher.findViewById(;

    View sectionHeading = inflater.inflate(R.layout.heading, list, false);
    TextView sectionHeadingTextView = (TextView) sectionHeading.findViewById(;


    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //Do something with it

Some adapter methods:

public int getViewTypeCount() {
    return TYPE_COUNT;

public int getItemViewType(int position) {
    return (position < items.size()) ? ITEM_NORMAL : ITEM_ADVANCED;

public Product getItem(int position) {
    return items.get(position);
public boolean areAllItemsEnabled() {
    return false;

public boolean isEnabled(int position) {
    return (position < items.size());

Is this the normal behaviour when adding a headerView?? And how to overcome the issues in my adapter?

Do not use your adapter, use 'decorated' adapter from getAdapter.

Hi we can resolve this in this way first you create your header view, after inject in your list and the last step unable the setOnClickListener. its works for me. any feedback welcome

ViewGroup headerView = (ViewGroup) getLayoutInflater().inflate(R.layout.your_header, list,false);

If you add a header to a ListView that becomes the first item in the list. You could compensate for this by overriding the getCount() to return one item less, but ensure you handle the getItem(), getItemId() and so on as the header would be the item in position 0.

If you don't care about the click to the header, subtract the number of header views from the position to get the position for your adapter:

                R.layout.my_hdr_layout, null), null, false);
        listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            position -= listView.getHeaderViewsCount();
            final MyObject object = m_adapter.getItem(position);


Not to add much new that @Ramesh didn't add but some extra context:

personList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick( AdapterView<?> parent, View view, int position, long duration) {
        Map<String, Object> person = (Map<String, Object>) parent.getAdapter().getItem(position);
        if (person != null){

            // If the header was clicked on a null  is returned

            OnPersonUiChangeListener listener = (OnPersonUiChangeListener) getActivity();

One solution to this is to map the id to index. Basically make id return the index in the list and the the click listener would do:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            listAdapter.getItem((int) id);

Use getSelectedItemPosition() Return the position of the currently selected item within the adapter's data set. This method don't need to worry about the size of headers or footers.

When implementing AdapterView.OnItemClickListener we just subtract the headers from the position.

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    position -= mListView.getHeaderViewsCount();
    // what ever else follows...