将页脚添加到自定义列表视图

add Footer to custom lsitview

如何使用 Adapter 向 listView 添加页脚?

<?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>

我已经试过了:

 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);
        }


    }

但是 returns java.lang.NullPointerException !

可能的问题:

充气器有问题!

上下文错误!

谢谢!

最简单的解决方案是定义一个包含 2 个项目的线性布局,第一个是列表视图,第二个是页脚。

如果您仍然坚持通过页脚方法使用页脚,您可以从 activity 代码:

//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);

调用方法

addFooterView (View v)

并将要添加的视图传递到列表视图。 列表视图已经有了这个方法。 看看 docs.