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被按時(shí)如何不出現(xiàn)灰色?
問題解答
回答1:很簡(jiǎn)單,但也很容易出錯(cuò)的問題,加了footer后,你的listview item數(shù)量是3,但adapter的viewcount其實(shí)并沒有變成3,所以在你點(diǎn)擊footer時(shí)執(zhí)行的是obj.getItem(2),肯定是數(shù)組越界異常了。對(duì)于添加了header或footer的listview,正確的取item方法應(yīng)該是
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時(shí)不越界,再做對(duì)象空判斷。
回答2:你不能使用setOnItemClickListener 來作為footview的點(diǎn)擊事件,我認(rèn)為你應(yīng)該單獨(dú)的去設(shè)置例如 footview.setonClickListener(new OnClickListener{}); 祝你好運(yùn)
回答3:你這個(gè)是數(shù)組下標(biāo)越界了啊,你的數(shù)組size是2,所以對(duì)應(yīng)的下標(biāo)只能是0和1,但是你在使用的時(shí)候用了2,錯(cuò)誤顯示你有個(gè)無效的index 2,你自己找下第61行和第88行,看是否有地方調(diào)用了index是2的
回答4:將footerLayout移出listView的寫法是
listview.addFooterView(footerLayout, null, false);
相關(guān)文章:
1. javascript - js 有什么優(yōu)雅的辦法實(shí)現(xiàn)在同時(shí)打開的兩個(gè)標(biāo)簽頁(yè)間相互通信?2. css3 - Typecho 后臺(tái)部分表單按鈕在 Chrome 下出現(xiàn)靈異動(dòng)畫問題,求解決3. java - 新手做一個(gè)安卓視頻播放器,想實(shí)現(xiàn)一個(gè)進(jìn)度條,按鈕那種在視頻下方懸浮的功能,不知道思路!4. javascript - jquery怎么給select option一個(gè)點(diǎn)擊時(shí)觸發(fā)的事件,如圖 如果選擇自定義觸發(fā)一個(gè)時(shí)間?5. nginx配置server模塊的問題6. java - android代碼重構(gòu):如何把a(bǔ)pp設(shè)置里的頭像UI做成通用的?7. node.js - express請(qǐng)求的具體方法8. javascript - angular和jquery都用到了$符號(hào),一起用會(huì)不會(huì)沖突?9. 想找個(gè)php大神仿個(gè)網(wǎng)站。10. javascript - 怎樣限制同一個(gè)瀏覽器不能登錄兩個(gè)賬號(hào)
