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

时间:2015-01-10 21:33:48

标签: android footer listadapter

如何将页脚添加到 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>

我试过这个:

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


    }

但它返回java.lang.NullPointerException

可能出现的问题:

inflater的错误!

上下文错误!

谢谢!

2 个答案:

答案 0 :(得分:1)

调用方法

addFooterView (View v)

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

答案 1 :(得分:0)

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

如果您仍然坚持使用页脚方法使用页脚,则可以从活动代码中执行此操作:

//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);
相关问题