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
被按时如何不出现灰色?
It’s a very simple but error-prone problem. Added
footer
后,你的listview item
数量是3
,但adapter
的viewcount
其实并没有变成3
,所以在你点击footer时执行的是obj.getItem(2)
,肯定是数组越界异常了。对于添加了header或footer的listview
, the correct way to get the item should beheader或footer属于AdapterView的子view
,listView.getAdapter().getItem(position);
It can ensure that you do not cross the boundary when taking the position of 2, and then make an object null judgment.You cannot use setOnItemClickListener as the click event of footview. I think you should set it separately
For example, footview.setonClickListener(new OnClickListener{}); Good luck to you
Your array subscript is out of bounds. Your array size is 2, so the corresponding subscripts can only be 0 and 1, but you used 2 when using it. The error shows that you have an invalid index 2. Look for lines 61 and 88 yourself to see if there is any place where the index is 2 is called
The way to write
footerLayout
移出listView
islistview.addFooterView(footerLayout, null, false);