Search code examples
androidandroid-fragmentsandroid-listfragment

Switch out empty ListView Views in ListFragment


I have a list fragment that does not inflate a custom xml. I'm doing this so that I get the spinning circle progress bar while my content loads from the internet. However, when I receive a network exception, I want to replace the ListView with a TextView that says "Can't be displayed, etc, Press anywhere on the screen to try a reload". How would I do this?

As it stands now, when a network exception is thrown, the circle progress loader just spins and spins and the user has no indication the connection failed. I want to do this IN the view, I don't want an Alert or a Toast, etc. I tried using setEmptyView(myTextView) - but it didn't work, the loading circle was still on the screen instead. And if I define the empty view in an Xml, then the 'loading circle' never shows, which is also not what I want. I want the circle to appear up until there is an exception, and then swap it out with that TextView.

Any ideas?


Solution

  • You can use setEmptyView("Error oh noo!"); to display your error like you were trying. You just need to display your ListView (with empty contents) for that empty view to show up.

    setEmptyView("Error oh noo!");
    if (isResumed())
        setListShown(shown);
    else
        setListShownNoAnimation(shown);