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:很簡單,但也很容易出錯(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. Python爬蟲如何爬取span和span中間的內(nèi)容并分別存入字典里?2. mysql - 把一個(gè)表中的數(shù)據(jù)count更新到另一個(gè)表里?3. 請(qǐng)教使用PDO連接MSSQL數(shù)據(jù)庫插入是亂碼問題?4. mysql - 分庫分表、分區(qū)、讀寫分離 這些都是用在什么場景下 ,會(huì)帶來哪些效率或者其他方面的好處5. visual-studio - Python OpenCV: 奇怪的自動(dòng)補(bǔ)全問題6. 視頻文件不能播放,怎么辦?7. mysql 查詢身份證號(hào)字段值有效的數(shù)據(jù)8. python - 《flask web 開發(fā)》一書,數(shù)據(jù)庫中多對(duì)多關(guān)系的實(shí)現(xiàn)問題?9. node.js - nodejs開發(fā)中常用的連接mysql的庫10. python - 爬蟲模擬登錄后,爬取csdn后臺(tái)文章列表遇到的問題
