---
title: "ListView添加headview或者footview时onItemClick事件数组越界的问题"
author: "hellorheaven"
date: "2014-11-20T09:32:38Z"
tags: []
categories: ["移动开发"]
slug: "listview-headview-footview-onitemclick"
draft: false
lastmod: "2014-11-20T09:32:38Z"
id: 2158332
url: "https://hellorheaven.iteye.com/blog/2158332"
---

项目中Listview需要添加headview以及footview.

响应listview的onitemclick事件的时候容易出现数组越界的问题.

排查后发现自己使用的是自定义的adater来获取对象.

需要调用parentadapter来调用，即可避免问题，示例代码如下：


```java

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    parent.getAdapter().getItem(position);
}

```



参考url:<http://blog.chengbo.net/2012/03/09/onitemclick-return-wrong-position-when-listview-has-headerview.html>