0

I have a ViewPager, that i'm populating with fragments which has a listview inside. The point is that i have to show a really big amount of data, and i need to make pages, so i only request littel amounts of data.

My problem is that i need to call an asynctask to retrieve the data when the page is changed, and fill the listview of this page, how can i do this? How can i change that listview in the onPostExecute of the task?

PS: i have used an eclipse template for tabs + swipe activity, so im not posting my code.

Edgar
  • 377
  • 1
  • 5
  • 23

1 Answers1

1

I'm not sure I got your problem right, but the first thing that comes to my mind, is that I would use an AsyncTaskLoader instead of a simple AsyncTask. From my (limited) experience, loaders solve a lot of problems when it comes to Fragment/Activity lifecycle/configuration changes.

Loaders guide (Android Developers)

No matter what method you are using to get the data, changing the content of the list view in page B after loading the data in page A shouldn't be much of a problem: you have plenty of options, from simply saving the data for page B in the Activity (then changing the page with setCurrentItem and intercepting the page change event with setOnPageChangeListener), to a more elegant approach employing a SQLite database (which btw would allow you to do some caching on the results, if possible).

Oh, and let's not forget that, if you are using an implementation of the abstract class FragmentPagerAdapter you can probably pass the data directly from one page to the other, as this PagerAdapter implementation

represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.

(just use getItem(int) to get a reference to the page you need. I never tried this myself, though).

Hope this helps

EDIT I just found out that getting a reference to the current Fragment shown in the ViewPager is tricky if you are not using a FragmentPagerAdapter: if this was the root of your problem, information about how it can be done can be found here:

Update data in ListFragment as part of ViewPager

Retrieve a Fragment from a ViewPager

Communication between ViewPager and current Fragment

Community
  • 1
  • 1
Rick77
  • 3,014
  • 23
  • 38
  • Thanks for the tip, i'll take a look at the loaders guide, but, i know i can do things with the SetOnPageChangeListener, but my problem is, how do i tell "change ListView on Fragment B" to the AsyncTask. The Data must be called exactly when the page is changed – Edgar May 28 '13 at 12:38
  • 1
    I think I got your problem now, and I have expanded my answer to match – Rick77 May 28 '13 at 14:57
  • Thank u!, i will be using this. – Edgar May 28 '13 at 16:24