Android adding footer to ListView addFooterView()?

42,317

Solution 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.

Solution 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.

Share:
42,317
Kman
Author by

Kman

Updated on July 05, 2022

Comments

  • Kman
    Kman almost 2 years

    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:

      <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" />
    
  • Kman
    Kman almost 14 years
    Hey thanks and your right it seems like loadMore is not being found...added more code hope that helps
  • MGSenthil
    MGSenthil over 13 years
    i hope that u may got the view,i need your help to set footer at bottom of my listview,initilly footer appears at bottom of listview and it have click option to load more,after click event the data has been loaded but the footer moves down on each click from footer.let help me to solve this issue.
  • Kman
    Kman over 13 years
    Not sure what you mean by "after click event the data has been loaded but the footer moves down on each click from footer" but what I'm doing after getting new data is creating a new adaptor and then calling 'setListAdapter()' again. However this makes the list start from the top so I adjust the position of the list by calling 'getListView().setSelection(newPosition)' Hope that helps!
  • Edward Brey
    Edward Brey over 9 years
    With SDK 21, you get a warning if you pass null to inflate. Instead, use mInflater.inflate(R.layout.list_load_more_row, getListView(), false).