Irfan Ahmed
Irfan Ahmed

Reputation: 9374

addHeaderView does not work when the list is empty

I am using following layout(header.xml) to add header in a listview,

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/greetingContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/categoryTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="header view"
    android:textColor="#FFFFFF"
    android:textSize="15sp" />

</LinearLayout>

and on the other side, i am using,

View header = (View) getLayoutInflater().inflate(R.layout.header,null);
getListView().addHeaderView(header);

when the list is empty it's not working, stays invisible..

my question may be the duplicate of this, but unable to understand,
please help!

Upvotes: 1

Views: 2025

Answers (2)

Sadegh
Sadegh

Reputation: 2669

Simple solution, all you need to do is that you should ALWAYS set an adapter for your listview, even when you do not have any data to show.

Upvotes: 3

krthk123
krthk123

Reputation: 23

I have used a similar header view and it is coming fine when there is no data in the listview.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.list, null);
    listView = (ListView) view.findViewById(R.id.listView1);

    if (listView.getHeaderViewsCount() == 0) {
        headerView = View.inflate(getActivity(), R.layout.listheader,     null);
        listView.addHeaderView(headerView, null, false);
    }
    return view;

}

Upvotes: 1

Related Questions