Search code examples
androidfooterlistadapter

add Footer to custom lsitview


How to add a Footer to a listView with Adapter ?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent>
<TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>
    <TextView
       android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
     </RelativeLayout>

I've try this :

 public class message extends ListActivity {
       List<SMSData2> smsList2 = new ArrayList<SMSData2>();
        private Context context = null;
        private ListView list = null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            list = (ListView)findViewById(android.R.id.list);

            //code to set adapter to populate 
for (int i=0;; i < 10; i++) {
                SMSData2 sms2 = new 
                sms2.setBody(c.getString("some text");
                smsList2.add(sms2);
                setListAdapter(new ListAdapter2(this, smsList2));
                        }
              View footerView =              ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
            list.addFooterView(footerView);
        }


    }

but it returns java.lang.NullPointerException !

possible issues :

bug with the inflater !

Bug with the context !

thanks !


Solution

  • simplest solutin is to define a linear layout that has 2 items first is your list view and the second is the footer.

    if however you still insist on using a footer by the footer method you do so from activity code:

    //after code to set adapter to populate list
            View footerView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
            list.addFooterView(footerView);