Where to add setEmptyView


#1

Not quite clear on if I need an xml layout for the listview or if i can manage all of this though code.

the book says that you can use this method or create a layout. can’t seem to find a way to reference listview in code. thanks! :question:


#2

found it, but it does make me curious why i would want an xml layout for the ListView ???


#3

Would you like to post your solution for the benefit of other people who have the same problem. Thanks.


#4

Having a separate layout file for the listview is handy if you want it to appear within a frame of some kind.

See my answer elsewhere for more info on the empty view - this is a view that exists somewhere in your hierarchy that ListView will hide or show for you. If there is an empty view and your ListView has no data, it will hide itself and show the empty view. When it does have data, it will hide the empty view, and show itself.


#5

Yea if you want to create both a textview with a message like (“There are no crimes.” AND you want to add a button for adding a crime ALL in the same “empty list” view stub. You should create a list layout file like so:

[code]<?xml version="1.0" encoding="utf-8"?>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
	android:layout_height="match_parent"
	/>

<FrameLayout
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <!--  android:id="@android:id/empty" !-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="@string/empty_crime_list"
        android:textSize="@dimen/empty_list_text_size"
        android:padding="4dp"
        />
    
    <Button
        android:id="@+id/button_add_crime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="4dp"
        android:gravity="center"
        android:maxLines="2"
        android:padding="5dp"
        android:text="@string/add_crime"
        android:textAlignment="center"
        android:textSize="@dimen/empty_list_text_size" 
        />

</FrameLayout>	
[/code]

In order for the listview adapter to know how to switch between an empty list view stub and the actual list view when crimes are added, you have to use specific id values:
for the populated list add a listview child to the root viewgroup (a framelayout in this case) with an id value of:
"@android:id/list"

then for the empty list (unpopulated list) I wrapped a textview and a button inside of a framelayout viewgroup with the id value of:
"@android:id/empty"

Then back in the crimelistFragment.java file within onCreateView, I added/edited the code as follows:

public View onCreateView(LayoutInflater inflater, ViewGroup parent,
			Bundle savedInstanceState) {
		
		View v = inflater.inflate(R.layout.empty_list_item_stub, parent, false);
		
		
		ListView view = (ListView)v.findViewById(android.R.id.list);
		view.setEmptyView(v.findViewById(android.R.id.empty));

The list adapter takes care of the switching for you based on the two IDs …list and …empty. Hope this helps.


#6

[quote=“PCfrustration”]Yea if you want to create both a textview with a message like (“There are no crimes.” AND you want to add a button for adding a crime ALL in the same “empty list” view stub. You should create a list layout file like so:

[/quote]

My understanding is defining “@android:id/empty” & “@android:id/list” in the XML is a complete solution in itself. Post adding these ids & inflating in onCreateView(); a call to setEmptyView() will make no difference and is redundant. Can someone please correct me if I am wrong?

Also, can’t figure out the solution which uses the setEmptyView() and not the XML solution, can someone who has cracked it post some pointers? Thanks!


#7

I have the same notion about adding android:id in xml file: once you have it there, android will automatically chooses between the two lists. Could someone explain that, please.


#8

Just need to inflate the layout xml file.

The following two lines are not required.
ListView view = (ListView)v.findViewById(android.R.id.list);
view.setEmptyView(v.findViewById(android.R.id.empty));


#9

I was confused with this as well. Played around a bit, and I think I figured out what the book is trying to say:

  1. You can make the IDs for the listview and empty view “list” and “empty”, respectively, then they’ll switch automatically.
    OR
  2. You can make your own view (using code or xml perhaps? I just used xml.) as the empty view, ID it with what you want, and use the method setEmptyView() using the custom ID to make android use that view instead of “empty”.

Please correct me if I’m wrong, just trying to get a better understanding.