How to make sure that all the ListView
entries are deleted and display the text " ListView is empty. "?
2 answers
Using the getCount()
method, you will find out the number of elements in the sheet and, depending on how many they are, perform the appropriate actions:
ListView list = (ListView) findById(R.id.list_id); int count = list.getCount();
If count = 0
, display a message!
- oneThank you very much, did - it works. I will add that to work with deleting and navigating through different activations, I also did the same check in the onStart () method, so that when adding and returning to the necessary activit, the inscription disappeared. - denAbra
To implement the function of a message that the list is empty, ListView
has the standard setEmptyView(View)
method:
You can set an arbitrary View
(in this TextView
example) that the list is empty from the code:
ListView lv = (ListView)findViewById(R.id.listView); View empty = findViewById(R.id.emptyList); lv.setAdapter(adapter); lv.setEmptyView(empty);
markup:
<RelativeLayout android:id="@+id/listLayout" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/emptyList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="EMPTY!"/> </RelativeLayout>
Through ViewStub
you can display the whole Layout
, which will be quite complicated: formatted text, a picture and so on.
<RelativeLayout android:id="@+id/listLayout" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <ViewStub android:id="@+id/emptyList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout="@layout/emptyLayout" /> </RelativeLayout>
Or, if the inheritance from ListActivity
is used, use the system ID ( @id/android:empty
) to report that the list is empty. This ID should be assigned to View
on the markup, which will show the message:
<RelativeLayout android:id="@+id/ListLayout" android:layout_height="match_parent" android:layout_width="match_parent"> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>