android - Error: java.lang.IndexOutOfBoundsException: Invalid index 2
问题描述
How to remove the seperator line in footerLayout? I have a footerLayout below the listView, used to display the totalAmount as shown below. If I click the seperator line in footerLayout, my app crashed.
My MainActivity
AllAdapter obj = new AllAdapter(getApplication(), search, listview,imageView,text,button);footerLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.under_listview, null);totalAmount = (TextView) footerLayout.findViewById(R.id.amount);
LogCat error
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) at java.util.ArrayList.get(ArrayList.java:304) at com.example.tony.monthlyexpenses.adapter.AllAdapter.getItem(AllAdapter.java:61) at com.example.tony.monthlyexpenses.QuickExpenses$1.onItemClick(QuickExpenses.java:88) at android.widget.AdapterView.performItemClick(AdapterView.java:301)
The error pointed to listView onClickListener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {mClickedPosition = position;Expenses o = (Expenses) obj.getItem(position);String day = o.getDate(); }});
AllAdapter
public Expenses getItem(int position) {return search.get(position); }
The footerLayout is supposed to be displayed outside the listView, not inside. How can I get rid of this ?
I also have activity_main.xml, AllAdapter class, all_adapter.xml for ListView and also under_listview.xml for the footerLayout.
activity_main
AllAdapter
under_listview
How to move the footerLayout out from the ListView ?
I add android:footerpidersEnabled='false' now become like this
But still clickable !!!
谁知道问题出在哪?
footerLayout被按时如何不出现灰色?
问题解答
回答1:很简单,但也很容易出错的问题,加了footer后,你的listview item数量是3,但adapter的viewcount其实并没有变成3,所以在你点击footer时执行的是obj.getItem(2),肯定是数组越界异常了。对于添加了header或footer的listview,正确的取item方法应该是
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {Expenses o = (Expenses) listView.getAdapter().getItem(position);if(o != null){ mClickedPosition = position; //Expenses o = (Expenses) obj.getItem(position); String day = o.getDate();} }});
header或footer属于AdapterView的子view,listView.getAdapter().getItem(position);能确保你取2的position时不越界,再做对象空判断。
回答2:你不能使用setOnItemClickListener 来作为footview的点击事件,我认为你应该单独的去设置例如 footview.setonClickListener(new OnClickListener{}); 祝你好运
回答3:你这个是数组下标越界了啊,你的数组size是2,所以对应的下标只能是0和1,但是你在使用的时候用了2,错误显示你有个无效的index 2,你自己找下第61行和第88行,看是否有地方调用了index是2的
回答4:将footerLayout移出listView的写法是
listview.addFooterView(footerLayout, null, false);
相关文章:
1. python相关问题求解决,有偿2. javascript - input输入框触发change事件后,外部元素会被选中3. python - 用__call__ 实现装饰器功能4. javascript - vue2.0中router.repalce跟router.push有什么区别,二者的应用场景是会有怎样的区别?5. javascript - prismplayer播放器全屏问题6. javascript - 在sublime中如何给.vue文件注释?7. python - 请问用cxfreeze打包的可执行程序能反编译出源码么?8. HTML5怎么访问本地文件?9. javascript - 这种上传图片预览怎么做?10. javascript - 把字符串日期转换成一般时间格式后,发现小于10的数字不会自动添加0在前面,如下