Reputation: 25204
I have:
ViewPager
with a FragmentPagerAdapter
on (or a FragmentStatePagerAdapter
, doesn't really solve my problem).TextView
s that need to be set differently;AsyncTask
that queries my database and retrieves content to be set into the TextViews.So my code was:
public class StatsPagerAdapter extends FragmentPagerAdapter {
static final int FRAGMENT_COUNT = 5;
private Parameters[] sectionData;
public StatsPagerAdapter(FragmentManager manager) {
super(manager);
this.sectionData = new Parameters[FRAGMENT_COUNT];
}
@Override
public Fragment getItem(int position) {
return StatsSectionFragment.getInstance(this.sectionData[position]);
}
@Override
public int getCount() {
return FRAGMENT_COUNT;
}
public void setSectionData(int position, Parameters sectionData) {
this.sectionData[position] = sectionData;
notifyDataSetChanged();
}
}
So I'm passing sectionData[position]
to the getInstance()
method of my generic sub-fragment. That data should differentiate each instance of the fragments loaded into the ViewPager
.
At first I'll be passing an empty reference, but then in my async class:
@Override
protected void onProgressUpdate(Parameters... sectionValues) {
super.onProgressUpdate(sectionValues);
mPagerAdapter.setSectionData(sectionId, sectionValues[0]);
}
That should call the setSectionData()
above, update sectionData[position]
and generate a call to notifiyDataSetChanged()
. I was hoping that doing so would make the adapter retrieve all its items again, thus calling getItem()
again, and loading new fragments.
Sadly it does not. So right now:
if fragments (i.e., getItem()
) are created before my async task result are published, that item will stay empty (i.e.,visible fragment, but with empty text views since I never called getInstance(non-null stuff)
.
That happens for fragment 0 && fragment 1.
if fragments are created after my async task has ended (fragment 2 to end, because getItem()
is called only when you reach that fragment by swiping), then the first and only call to getItem()
produces a getInstance(non-null stuff)
, and that's ok.
How can I reload content into those already-there fragments, or force the adapter to call getItem()
and update its views?
Upvotes: 1
Views: 1752
Reputation: 18977
Add this to your adapter:
@Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
This make your adapter call getItem
again when you call notifyDataSetChanged();
Upvotes: 1