Android footer after listview

北城余情 提交于 2019-12-11 18:19:16

问题


I am facing a problem of application crash when I wanted to display a footer button underneath the list view. I have attached my xml code and activity code herewith. Any help will be highly appreciated.

ContactListActivity.java

  public class ContactListActivity1 extends ListActivity {
    static ArrayList<String> ids = new ArrayList<String>();

     ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ContactList1 contactList = this.getContacts();
        ArrayAdapter<Contact1> adapter = new ContactAdapter1(this,
                contactList.getContacts());


          // LoadMore button
            Button btnLoadMore = new Button(this);
            btnLoadMore.setText("Load More");

        View v = getLayoutInflater().inflate(R.layout.contactlistitem,null);

        lv.addFooterView(btnLoadMore);
        lv.setAdapter(adapter);



    }

And my XML code

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"


>

    <TextView
        android:id="@+id/txtDisplayName"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        >

</TextView>


     <ImageView

android:id="@+id/contact_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"


/>



   <ListView
    android:id="@+id/contactList1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/txtDisplayName"
    />



</LinearLayout>

回答1:


Yes, You made a mistake -

  • You're extending ListActivity so where is lv = getListView() in your code.

  • Or, you should give list = (ListView)findViewById(android.R.id.list); after your super.onCreate(savedInstanceState); in your onCreate

Because, extending ListActivity these are important. It seems not proper with your code. You better have look at this example How to add a footer in ListView? before starting your application.



来源:https://stackoverflow.com/questions/12084768/android-footer-after-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!