addHeaderView does not work when the list is empty
Asked Answered
G

2

1

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!

Glyceride answered 10/12, 2013 at 9:57 Comment(5)
You can use a Relative layout have text view at the top and listview below the textview. yes it won't show when listview is emptyAndyane
Everything is fine, when the list contains data!Glyceride
yes it is meant to work that way by default that is how it works.Andyane
how can i set a header when the list is empty then?Glyceride
read by first comment or look at this #18127632Andyane
G
3

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.

Grizzly answered 1/1, 2016 at 15:22 Comment(1)
This worked for me and should probably be marked as the correct answer. Just set an ArrayAdapter with an empty dataset.Invalidate
G
1

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;

}
Gage answered 10/12, 2013 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.