views:

2312

answers:

2

Android empty list layout

Whatever I do, I can't get the empty tag to display. Inside my tabs, I've got:

<LinearLayout
android:id="@+id/thisParticularTab"
android:orientation="vertical"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<TextView android:id="@+id/LastUpdated"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:text="List last updated: N/A" />

<ListView 
android:id="@+id/mySpecialListView"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" />

<Button id="@android:id/empty"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:text="Click to fill the list" /></LinearLayout>

However despite my ListView being empty (no items are displayed, and the database table being read from is empty), the button is not displayed. Any ideas?

Also, when it appears, will I be able to refer to R.id.empty? Does this really mean I can only have 1 ListView/empty per activity? My understanding is that 'id' is used to define UI define; how can I give it a name and also assign it as the empty View within the XML? (I'd prefer not to use setEmptyView())

+4  A: 

What you've missed here, is that the button id also needs the android prefix:

<Button 
   android:id="@android:id/empty"
   ... />

@android:id/empty refers to android.R.id.empty, not R.id.empty.

It is true that you should only have one element of a given id per view. If you are creating your own list view item, you naturally have the same elements (and subsequently the same ID's) over and over, but in these occasions you can access them by myListItem.findViewById(id)

More related to your scenario, if you would find the need to add another listview, which takes another empty-tag, and still want the listview to inherently handle the hiding and showing of that item, you can use the function setEmptyView(id)

David Hedlund
It was my understanding that "android:id/empty" id tag does this automatically in the XML; or do I still need to code in a check to see if the ListView is empty?
Chris
you're right, i misrea your code first time round. see my edits
David Hedlund
Thanks! Unfortunately this doesn't work either; I wonder if it's because it's so 'deep' (<TabHost><LinearLayout><TabWidget><FrameLayout><LinearLayout><ListView>).Using the setEmptyView(View) function works just perfectly - I suppose it doesn't look beautiful, but ships :)
Chris
A: 

Try:

<Button id="@id/android:empty"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:text="Click to fill the list" />

And to find the button:

 Button empty = (Button) findViewById(android.R.id.empty);
peter