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. node.js - vue-cli無法創(chuàng)建項(xiàng)目。2. javascript - h5微信中怎么禁止橫屏3. index.php錯(cuò)誤,求指點(diǎn)4. angular.js - angularjs 百度統(tǒng)計(jì)怎么統(tǒng)計(jì)5. angular.js - 可以通過vue或者angular雙向數(shù)據(jù)綁定iframe元素嗎?6. java - 計(jì)算機(jī)圖像表示方法?7. css3 - 這種情景怎么解釋?display:flex 遭遇 white-space: nowrap;8. PHP單例模式9. objective-c - 微信支付的問題10. Html 入門教程視頻無法播放
