Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setEmptyView on ListView not showing its view in a android app?

I have a problem with the setEmptyView method from a 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 don't see the TextView.

like image 627
gurehbgui Avatar asked Sep 18 '12 19:09

gurehbgui


1 Answers

Your TextView should be placed right under the ListView item with its visibility set to gone (android:visibility="gone"), do not place it in another layout. This is how your main layout would look like

<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 this 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); 
like image 159
mmbrian Avatar answered Sep 21 '22 23:09

mmbrian