android listview footer view,Android adding footer to ListView addFooterView()?

问题

I have a ListView activity that needs a footer to the list of items so that you can click it and it would load more items into the list. The list is backed my an SimpleAdapter backed by a map of strings and before the adapter is set i do this inorder to add the footer:

mInflater.inflate(R.layout.list_load_more_row, null);

TextView footer = (TextView) findViewById(R.id.loadMore);

getListView().addFooterView(footer);

setListAdapter(ListViewHelper.getAdapterForContentList(mContent, this));

But Im getting this exception in the debugger

java.lang.NullPointerException

android.widget.ListView.clearRecycledState(ListView.java:489)

android.widget.ListView.resetList(ListView.java:476)

android.widget.ListView.setAdapter(ListView.java:417)

Whats wrong and how would i go about adding my footer to the list?

[EDIT]

the activity is using

list_load_more_row.xml:

android:id="@+id/loadMore"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="17sp"

android:textStyle="bold"

android:textColor="#000000"

android:ellipsize="marquee"

android:marqueeRepeatLimit="marquee_forever"

android:gravity="center_horizontal|center_vertical"

android:text="@string/hello" />

回答1:

Alrighty Iv found a solution to my problem if i do this it works as intended:

View view = mInflater.inflate(R.layout.list_load_more_row, null);

TextView footer = (TextView) view.findViewById(R.id.loadMore);

getListView().addFooterView(footer);

setListAdapter(ListViewHelper.getAdapterForContentList(mContent, this));

I'm guessing that since I put null in the inflater as the parent parameter, the view has not been added to the current content view and so mainActivity is unable to find it and now since I am explicitly using the parent view that is returned by the inflater to find the TextView it is working.

回答2:

as ognian stated in his comment above, loadMore is probably not found.

you can see if this is the issue by changing your code to something like this:

TextView footer = (TextView) findViewById(R.id.loadMore);

if ( footer != null ) {

getListView().addFooterView(footer);

setListAdapter(ListViewHelper.getAdapterForContentList(mContent, this));

} else {

throw new NullPointerException("footer is null");

}

without seeing more of your code, it is hard to say what the actual cause is.

回答3:

View footerView = getLayoutInflater().inflate(R.layout.search_footer,

mContentList, false);

LongButton mSearchMoreBtn = (LongButton) footerView

.findViewById(R.id.searchmore_btn);

mSearchMoreBtn.setText(R.string.search_moreapp);//the button to add

mSearchMoreBtn.setBackgroundResource(R.drawable.btn_long_selector);

mSearchMoreBtn.setOnClickListener(mSearchMoreBtnListener);

footerView.setOnClickListener(mLoadMoreAppsListener);

mContentList.addFooterView(footerView);

I have no idea that when I adding mSearchMoreBtn to ListView as footerView, it is wrong, instead, when I adding footerView to ListView, it just fine

来源:https://stackoverflow.com/questions/3227182/android-adding-footer-to-listview-addfooterview

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值