Reputation: 191
I want to make multiple empty views for a list view and set them programmatically. So I 've got a listview in an ListActivity. The way my client wants the app, I have a header bar in the app, so the layout looks like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/providerListLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="@+id/headerBar_ref"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/header_with_dashboard_button" />
<include
android:id="@+id/loadingView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
layout="@layout/loading_view" />
<RelativeLayout
android:id="@+id/listViewWrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/headerBar_ref" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/headerBar_ref" >
</ListView>
</RelativeLayout>
So I have the 2 empty views in separate xml files. In the list Activity I try to set the empty view like so:
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.listViewWrapper);
RelativeLayout noFavsLayout = (RelativeLayout) this
.getLayoutInflater().inflate(emptyViewLayoutId,
rootLayout);
getListView().setEmptyView(noFavsLayout);
But when I do this, the empty view is there ALL the time. I've also tried to to add the view using addContentView(), but that takes over the whole screen. I've not been able to find a solution on S/O yet
Upvotes: 1
Views: 709
Reputation: 190
Based on reading http://wiresareobsolete.com/2010/08/adapting-to-empty-views/, the actual mechanism for showing the empty view is that the adapter checks to see if the list is empty, and then sets the visibility of either the ListView or empty view to View.GONE, then sets the other one to View.VISIBLE. For this to work properly, both views have to be in the same parent view. In your example, this would mean something like
<RelativeLayout
android:id="@+id/listViewWrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/headerBar_ref" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This list is empty."
/>
</RelativeLayout>
(Note that I removed "layout_below" from your ListView, it was the only item in the relative layout, so it didn't need that reference. Also, the view has been added in the XML, you should not have to inflate it in the java.)
Now if you want to programmatically set a different empty view (for instance after performing a search), you could add another view to your relative layout, with another id (such as noResults)... and discover that it's always displayed.
<TextView
android:id="@+id/noResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No results were returned."
/>
So in your onCreate() you'll need to find that view and set its visibility to gone as well.
ListView listView = (ListView)findViewById(android.R.id.list);
View empty = findViewById(android.R.id.empty);
listView.setEmptyView(empty);
View noResults = findViewById(R.id.no_results);
noResults.setVisibility(View.GONE);
Then whenever you change the empty view for your list, you'll want to set the visibility of the other view to GONE to make sure only one is getting displayed.
listView.setEmptyView(noResults);
empty.setVisibility(View.GONE);
I hope this helps!
Upvotes: 1