You may have an empty view without ListActivity ! The correct method is as follows
First add an โempty viewโ to your XML layout under your list
... <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <TextView android:id="@+id/empty" android:text="Empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" /> ...
Next, override the onContentChanged method of your activity and set the empty view of your list to empty:
@Override public void onContentChanged() { super.onContentChanged(); View empty = findViewById(R.id.empty); ListView list = (ListView) findViewById(R.id.list); list.setEmptyView(empty); }
What is it! Android will take care to hide / show the list and empty views when updating the adapter.
Magic
The decision about whether an empty view is displayed or not is handled by the ListView superclass, AdapterView . AdapterView registers a DataSetObserver on the adapter, so it is notified of every data change. This calls the checkFocus call in the AdapterView , which contains the following lines
if (mEmptyView != null) { updateEmptyStatus((adapter == null) || adapter.isEmpty()); }
and sets the visibility of the empty view depending on whether the adapter is empty or not.
Joseph Earl Apr 6 2018-11-11T00: 00Z
source share