Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show header of ListView when its empty

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

like image 927
MarutiB Avatar asked Aug 08 '13 13:08

MarutiB


People also ask

How check ListView is empty?

just check if (cartlist. size()<0) then yours list is Empty.!

How do I add a header to a list view?

This example demonstrates How to add header item for Listview in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is the use of ListView explain list view with example?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.


2 Answers

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.

like image 107
Jesus Christ Avatar answered Oct 03 '22 23:10

Jesus Christ


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.

like image 25
MarutiB Avatar answered Oct 04 '22 00:10

MarutiB