views:

62

answers:

3

Hi All,

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  A: 

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.

BrennaSoft
@BrennaSoft: I was adding a item to arraylist saying the list is empty. But I felt it was not a clean implementation. Also my row of the list has now got an image too. That is why I posted this question.
rajaramyadhav
+1  A: 

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.

alexanderblom
Thanks a lot Alexander. This was exactly the same feature that I was looking for.
rajaramyadhav
A: 

If it's just text you want to show, specify it in android:hint for the ListView. It'll be displayed when the ListView has no items.

Dave
Dave, I tried this option. For some reason, Eclipse did not list android:hint as one of the options when I used Content assist (Ctrl + space) feature of Android. You have any ideas about this?
rajaramyadhav
Hmm, you're right, it doesn't exist. I was sure I had used it before, so maybe I was thinking of something else. My apologies.
Dave
Thanks for clarifying this Dave..
rajaramyadhav