Challenge Problem


#1

So I have completed the challenge however the last line says “Add a button to the view that will trigger the creation of a new crime so that, in this case, the user does not have to go to the options menu or action bar.”

I understood that the following could be used to set android.R.id.empty to display only when android.R.id.list doesn’t contain any entries (is that right?)

view.setEmptyView(v.findViewById(android.R.id.empty));

The problem is that when I add a button, this doesn’t disappear when I have entries because you can only set 1 view with the name android.R.id.empty. Im guessing I have to some how group these together so they disappear together but the button still has to have an id I can lookup to set the OnClickListener(). How do I do this?

Please see my code below


View v = inflater.inflate(R.layout.fragment_list, parent, false);

		ListView view = (ListView) v.findViewById(android.R.id.list);

		view.setEmptyView(v.findViewById(android.R.id.empty));

		Button mAddCrimeButton = (Button) v.findViewById(R.id.addButton);
		mAddCrimeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Crime crime = new Crime();
				CrimeLab.get(getActivity()).addCrime(crime);
				Intent i = new Intent(getActivity(), CrimePagerActivity.class);
				i.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getId());
				startActivityForResult(i, 0);

			}

		});

and my XML looks like this


<?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="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    
    <TextView
        android:id="@+android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:text="There are no crimes" />
    
    <Button
         android:id="@+id/addButton"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:text="Add a crime" />

</FrameLayout>

#2

So as an experiment I changed

view.setEmptyView(v.findViewById(android.R.id.empty));

to the following

view.setEmptyView(v.findViewById(R.id.addButton));

which seems to have done nothing, I would have expected my button to disappear as its the empty view but it doesn’t work like that. so if the empty view is decided by android.R.id.empty what does setEmptyView even do?

Totally confused


#3

ListFragment is very aggressive about resetting your list’s empty view. So if you have something tagged with android.R.id.empty, calling setEmptyView will have no effect.


#4

So is there a way I can group my TextView & Button so that they are both only displayed when the ListView is empty or do I need to manually detect this and mess with setVisable for the button?


#5

You can group them under a layout, and then have the layout be the empty view.