SetEmptyView in ListView does not show its view in Android application

Hi, there is a problem with the setEmptyView method from ListView.

Here is my Java code:

ListView view = (ListView)findViewById(R.id.listView1); view.setEmptyView(findViewById(R.layout.empty_list_item)); ArrayAdapter<Session> adapter1 = new ArrayAdapter<Session>(this, android.R.layout.simple_list_item_1, android.R.id.text1, MainController.getInstanz().getItems()); view.setAdapter(adapter1); 

empty_list_item:

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/emptyList" > </TextView> 

Listview

 <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > 

What is wrong with my code? When I have items, I see them in the list. But when the list is empty, I do not see the TextView .

+43
android listview
Sep 18 '12 at 19:16
source share
12 answers

The problem is that I have to do addContentView:

 View empty = getLayoutInflater().inflate(R.layout.empty_list_item, null, false); addContentView(empty, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); view.setEmptyView(empty); 

now its thin

+31
Sep 18
source share
— -

Your TextView should be placed directly below the ListView with visibility (android: visibility = "gone"), do not place it in another layout. This will look like your main layout

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listViewFangbuch" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> <TextView android:id="@+id/empty_list_item" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" android:text="@string/emptyList" > </TextView> </LinearLayout> 

And here is how your code might look like

 ListView view = (ListView)findViewById(R.id.listViewFangbuch); view.setEmptyView(findViewById(R.id.empty_list_item)); ArrayAdapter<Session> adapter1 = new ArrayAdapter<Session>(this, android.R.layout.simple_list_item_1, android.R.id.text1, MainController.getInstanz().getItems()); view.setAdapter(adapter1); 
+92
Sep 18 '12 at 19:47
source share

For me, none of these answers worked for me. I needed to add an empty view manually (inflated) to the "parent" view of Listview:

 ListView my_list = (ListView) findViewById(R.id.my_list); View emptyView = getLayoutInflater().inflate(R.layout.empty_view,null); ((ViewGroup)my_list.getParent()).addView(emptyView); listView.setEmptyView(emptyView); 
+30
Jul 23 '13 at 10:16
source share

The easiest way to use setEmptyView() is with your "empty" TextView and ListView in the same layout as here:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/empty_list_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/emptyList" android:visibility="gone" /> </LinearLayout> 

Now you can use:

 view.setEmptyView(findViewById(R.id.empty_list_item)); 
+18
18 Sep '12 at 19:24
source share

Having tried all the solutions presented here and experimented, I believe that the code below is the best method to use when your ListView and empty view are not side by side in a layout with a set of automatic identifiers.

 ViewGroup parentGroup = (ViewGroup)getListView().getParent(); View empty = getActivity().getLayoutInflater().inflate(R.layout.list_empty_view, parentGroup, false); parentGroup.addView(empty); getListView().setEmptyView(empty); 

Reasoning:

  • Passing the parent group into a bloat takes into account the layout_ * attributes of your empty view layout
  • Without attaching the view to inflate (false parameter), an empty view is returned. Otherwise, inflate will return the parent, requiring us to use parentGroup.findViewById (empty_view_id) to get an empty view for use with setEmptyView (). Here we avoid the additional search, the need for a different identifier and the need to disclose this identifier in our code. If we did not need to keep the link empty, then to say that inflation to join would be the correct action, eliminating the need to call addView.
  • The anti-template, addContentView (), is not the right approach. It will seem equivalent if your ListView occupies the entire visible space of the Activity. Instead of having your empty view be a sibling in ListView, it ends up being a sibling on other root level mockups in your activity. It seems to work because it floats (z-order) on top of all other species in the activity. See: Activity.addContentView (View) == ViewGroup.addContentView (View)?
+13
Nov 09 '14 at 20:02
source share

The core of the problem is that the AdapterView class does not actually add the view that you pass setEmptyView () to any container. It just changes the visibility. There are many ways to put an empty view object in a container, as other answers describe.

The documentation for AdapterView needs to be truly updated to reflect this, as this is not obvious.

+6
02 Oct '13 at 19:04 on
source share

Create a layout in XML that defines both the ListView and the empty view you want to use. If you give them the identifiers @android:id/list and @android:id/empty , android will automatically use the view with id:empty when the list is empty.

+4
Aug 04 '13 at 8:47
source share

There are some good solutions, but I think this is the best approach!

 View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null); addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView listView.setEmptyView(emptyView); listView.setAdapter(adapter); 
+3
Apr 23 '14 at 21:09
source share

also these two methods helped me display an empty view:

  adapter.notifyDataSetChanged(); list.invalidateViews(); 
+1
Jul 30 '14 at 9:00
source share

Just use

 View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null); ((ViewGroup)listView.getParent()).addView(emptyView); 

Instead of listView.setEmptyView(emptyView);

+1
Aug 22 '14 at 12:00
source share

My problem was that I added a footer that didn't seem to work with an empty view. After deleting the footer, an empty view is displayed.

0
Mar 25 '15 at 7:21
source share

I just learned android, but a snippet from https://developer.android.com/guide/topics/ui/layout/listview.html depicts a different approach:

 // Create a progress bar to display while the list loads ProgressBar progressBar = new ProgressBar(this); progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER)); progressBar.setIndeterminate(true); getListView().setEmptyView(progressBar); // Must add the progress bar to the root of the layout ViewGroup root = (ViewGroup) findViewById(android.R.id.content); root.addView(progressBar); 

ViewGroup root = (ViewGroup) findViewById (android.R.id.content);

0
Nov 29 '15 at 0:48
source share



All Articles