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.