How to show header of ListView when its empty
Asked Answered
S

4

6

I am developing the following screen

enter image description here

The fourboxes next to each other are buttons. On clicking of the buttons I am changing the adapter of the listview.

Since the content above the listview took up lot of space I made the whole thing as an header and added it via code.

   myPollsList = (ListView) findViewById(R.id.listArea);
myPollsList.addHeaderView(getLayoutInflater().inflate(R.layout.profile_listview_header, null));

Now I want to show some view when the list is empty. But if I do that then the header also goes away.

I am using this in a Activity and not a ListActivity. Any suggestions or workarounds for showing the header even when the list is empty ?

EDIT: Refer to my earlier question here ListView not getting space to show content on smaller screens . This is where someone suggested me to solve the problem by putting it as a header

Symposiarch answered 8/8, 2013 at 13:20 Comment(0)
S
12

So this is how I solved it. I had a custom adapter which was connected to the listview. When it found that it had zero items to display. It would add a fake empty view as an item to the list.

Symposiarch answered 9/8, 2013 at 10:19 Comment(1)
Aghhh... All this hacks we have to do to make something that should be basic functionality work! It drives me crazy!Buckram
T
28

I found a simple solution for this problem. If there's no elements for list and you are not adding the adapter, just add this:

mListView.setAdapter(null);

and the header will appear. It's easier than adding empty / fake item to the list.

Tramline answered 23/6, 2015 at 14:21 Comment(1)
thanks its work for me after adding these lines after addHeaderView() methodHint
S
12

So this is how I solved it. I had a custom adapter which was connected to the listview. When it found that it had zero items to display. It would add a fake empty view as an item to the list.

Symposiarch answered 9/8, 2013 at 10:19 Comment(1)
Aghhh... All this hacks we have to do to make something that should be basic functionality work! It drives me crazy!Buckram
P
2

maybe you will like this Android addHeaderView disappears when no items in ListView

override/subclass your adapter to return false from isEmpty()

Performing answered 28/10, 2014 at 8:40 Comment(0)
C
0

Don't use top layouts of button as header for list view.

So to adjust UI without using list header use weights.

Give parent Linear layout weight sum as 4 and 1, 1 to top Layouts and 2 for list view.

Chanellechaney answered 8/8, 2013 at 13:33 Comment(4)
Will that reduce the height of the top layouts ?Symposiarch
That is not what I want. I want the height to be the same across all devices. That is why I chose the header route so that the height of the header remains fixed. And the rest of list view can be scrolled downSymposiarch
as a proportion your all 3 views will be same size like 1:1:2 and also it will work great on any device just try it. cheersChanellechaney
Nope. This is not the effect I wanted. Thanks for the help :)Symposiarch

© 2022 - 2024 — McMap. All rights reserved.