Reputation: 536
I'm using a custom SimpleCursorAdapter. Currently my list stays empty, if no entries can be found in the database/cursor. Now, I want to display a message within the list, if there are no entries within the cursor/database. How can I handle this event?
Upvotes: 1
Views: 2621
Reputation: 435
Just place the view you want displayed in your XML, and give it any id you like, eg:
<ListView
android:id="@+id/myList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- view to be shown/hidden on empty list above -->
<TextView
android:id="@+id/emptyListElem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Nothing to show!" />
Then in your Activity's onCreate() method, call setEmptyView() on your listView object. The ListView should handle showing/hiding your view object, depending on whether the list is empty or not.
View empty = findViewById(R.id.emptyListElem);
listView.setEmptyView(empty);
Upvotes: 6
Reputation: 11923
If your ListView is expressed in xml, you can add a TextView with a specific android:id which will automatically display your message if the list is empty, as below:
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" />
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:text="No Data exist"
android:textSize="16dip"
android:textColor="@color/black"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>
You must use the @id/android:empty for this to work.
Upvotes: 3