Java Android 将页脚添加到 ListView addFooterView()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3227182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:34:39  来源:igfitidea点击:

Android adding footer to ListView addFooterView()?

javaandroidxmllistviewuser-interface

提问by Kman

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:

我有一个 ListView 活动,它需要项目列表的页脚,以便您可以单击它并将更多项目加载到列表中。该列表由一个由字符串映射支持的 SimpleAdapter 支持,在设置适配器之前,我这样做是为了添加页脚:

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)

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:

[编辑] 活动正在使用 list_load_more_row.xml:

  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        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" />

采纳答案by Kman

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.

我猜是因为我在 inflater 中将 null 作为父参数,视图还没有被添加到当前的内容视图,所以 mainActivity 无法找到它,现在因为我明确使用了返回的父视图inflater 找到它正在工作的 TextView。

回答by Ryan Conrad

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

正如奥尼安在他上面的评论中所说的那样,可能没有找到 loadMore 。

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.

如果没有看到更多的代码,很难说出真正的原因是什么。

回答by Logan

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 mSearchMoreBtnto ListViewas footerView, it is wrong, instead, when I adding footerViewto ListView, it just fine

我不知道当我添加mSearchMoreBtnListViewas 时footerView,这是错误的,相反,当我添加footerView到 时ListView,它就好了