2

I have a ListView that retrieves its data from the web. I want the ListView to show a spinning progress bar while it's retrieving data from the web and if there is no data fetched, I want to show a simple text 'No Data', else it will display the fetched data.

I looked for examples every where and even Google suggests using the Progress Bar as the empty view for the list. (See this example)

Doesn't really make sense as I require a Loading View + Empty View. Is there any example out there that describes how to do this? Or maybe anyone can clarify the proper steps to implement this?

prettyvoid
  • 3,446
  • 6
  • 36
  • 60
  • Just use a TextView for the EmptyView and a Dialog for the Progress, possibly driven by an AsyncTask. – Phantômaxx Oct 21 '14 at 16:10
  • I can't use a dialog, I'm specifically looking to use a ProgressBar. All of what I'm creating is enclosed within a view pager with tabs. The tabs contain fragments that contain ListViews. Each ListView will manage it's own ProgressBar. – prettyvoid Oct 21 '14 at 16:12
  • OK, the ProgressBar can be put in the ListView header. Or in its footer. Or have the ProgressBar in the header and a writing "Loading..." in the footer. Or vice-versa. – Phantômaxx Oct 21 '14 at 16:17
  • See my reply to Anx. Thanks. – prettyvoid Oct 21 '14 at 16:53
  • 2
    OK, the header and footer can be mage visible/invisible/gone, as per need. They can be EXTERNAL to the ListView (just put them above and/or below your ListView, not adding them by addHeaderView/addFooterView). – Phantômaxx Oct 21 '14 at 17:06
  • 1
    I've made a temporary (maybe permanent?) solution like you suggested, basically I added a FrameLayout that contains a ProgressBar below the ListView, and I set it initially to GONE. When I perform the http request I make it VISIBLE, and when result comes back I make it GONE again. I was hoping for a solution that is more friendly (and maybe more re-usable) but this should do for now. If anyone else have suggestions I'd love to hear them. Thanks Funky – prettyvoid Oct 21 '14 at 20:14

1 Answers1

1

You could create a layout to use for the "loading" view (an absolute layout with your indeterminate progress bar and text view, for example) and use it as the footer view for your ListView.

Then just add and remove the footer as needed.

// get reference to the footer view
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);

//add the footer view
listView.addFooterView(footerView);

//remove the footer view
listView.removeFooterView(footerView);

Reference 1, Reference 2

Community
  • 1
  • 1
AnxGotta
  • 1,006
  • 7
  • 28
  • Funkystein already mentioned that above, while it's a cool idea it comes with a lot of design limitations. I'm trying to have a design where the loading is centered in the middle of the listview. Not on top or bottom. Footer shows on top if the list is empty too. Also worth mentioning that Footer + Header count as a row in the listview, this means if you have a header and you want to retrieve the first REAL row, you'll do .get(1). It's not a big issue if I go with this approach however because the footer/header will only be displayed while fetching data, Still the design limitations though. – prettyvoid Oct 21 '14 at 16:39
  • Would it be an issue to have the progress bar overlap (basically on top of) the listview? Also, are you looking for a loading view to use for the initial loading of data only or do you need it to show/hide as you load more data into the list with existing data? – AnxGotta Oct 21 '14 at 19:32
  • See my reply to Funky above. My main objective is to show loading initially when there is 0 items in the list (before update). On update I have a swipe refresh layout that shows it's loading. Thanks for your suggestions – prettyvoid Oct 21 '14 at 20:16
  • Ah, I didn't see that. Funky has the correct answer for your needs. I thought you were loading in data continuously, like an infinite scroll. – AnxGotta Oct 21 '14 at 20:23