Empty view displays ok but does not disappear


#1

I can get the empty view to display ok, but when I add a crime, the empty view does not disappear, it stays there superimposed on top of the list.

I’m using a layout file as the book suggests - a FrameLayout containing a ListView and a TextView:

Also, I found that the ListView has to have an id of “android:list” otherwise the app crashes.

Any ideas as to why the empty view does not disappear?

My layout (a file called list_all_crimes.xml)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
	<ListView android:id="@+id/android:list"
	    android:layout_width="match_parent"
    	android:layout_height="match_parent"
	    />
	    
    <TextView
        android:id="@+id/empty"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
        android:text="Click the + icon to add a new crime"
        />
    
</FrameLayout>

In CrimeListFragment.java I’m inflating the xml layout in the onCreate method:

@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.list_all_crimes, parent, false);

#2

Update: I found the answer here:

http://stackoverflow.com/questions/3771568/showing-empty-view-when-listview-is-empty

Apparently the id for the empty view has to be: @android:id/empty

Praise for the book: I’ve got every Android programming book published in the last 2 years. I was struggling with most of them. This book is head-and-shoulders (on a stepladder) above all of them. Probably the best technical book I’ve read since ‘Head First Java’ and that is saying something.

Personal gripe about GUI programming: I know the value of separating code from layout, and xml is a good way of specifying things. BUT in practice, the small amount of xml always gives me more headaches than all of the procedural code. It’s so damned picky, the xml is not validated, there’s no easy way to debug it. Yuck! In real life I would use procedural code to set the empty view, however I’m attempting the xml technique in order to get more practice. OK. Rant over. I feel better now :slight_smile:


#3

Check out my answers elsewhere about setEmptyView. Honestly, I think it’s a little easier to see what’s going on when you use android.R.id.empty instead of setEmptyView. Everyone gets tripped up by the fact that setEmptyView expects the view to already be in the view hierarchy somewhere.

As far as the XML goes, it does get better as you gain an understanding of how the resource system works, and it definitely gets easier if you lean on your IDE to help take care of all the typing (and potential for mistyping) that is required. And it is definitely better than using code to layout a view hierarchy.

Thank you very much for the praise. We put a lot of ourselves into this book, and your appreciation means a lot to us.