How to display a message when the list view is emp

2020-07-27 02:36发布

问题:

I am a new to Android stuff. I am creating an application that contains a list view that will get dynamically populated. My requirement is when the list is empty, I would like to show a message. I don't want to create additional views just for displaying this message. Is there any nice way to do this? Any suggestions?

回答1:

ListActivity has a feature where it shows a specific view when the list is empty: @android:id/empty. You can easily just use a custom layout with ListActivity to achieve this.

If you don't want to use ListActivity you can always look at the implementation of ListView.java and adapt that.



回答2:

There really isn't a way to do it without creating extra views.

If your List view item is just a TextView you can always add an item to your adapter that says "the list is empty" and just make sure you clear the adapter before you add the real results.

Or you can add a TextView as the HeaderView that also says "the list is empty" and make sure to hide the header when you add the real results. I think the first option might be the best best.