1

Following this question:

How to recreate all fragments in ViewPager:

I have those classes:

public class ViewPagerAdapter extends FragmentPagerAdapter 
{
private List<Fragment> fragments;

/**
 * @param fm
 * @param fragments
 */
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

/* (non-Javadoc)
 * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
 */

@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

/* (non-Javadoc)
 * @see android.support.v4.view.PagerAdapter#getCount()
 */

@Override
public int getCount() {
    return this.fragments.size();
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}  
}

and a FragmentActivity:

 public class TabsViewPagerFragmentActivity extends FragmentActivity implements  
 ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener 
 {
    static final String TAG = TabsViewPagerFragmentActivity.class.getSimpleName();
    private TabHost mTabHost;
    private ViewPager mViewPager;
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String,      TabsViewPagerFragmentActivity.TabInfo>();
    public ViewPagerAdapter mPagerAdapter;
    .....

Now in this FragmentActivity I have this listener:

  private class SlicerSelectedOnClickListener implements OnClickListener 
{
    private ODSharedSlicer slicer;
    private int slicerIndex;

    public SlicerSelectedOnClickListener(ODSharedSlicer slicer, int slicerIntex)
    {
        super();
        this.slicer = slicer;
        this.slicerIndex = slicerIntex;
    }

    public void onClick(View v) 
    {
        slicerOnClickConfiguration(slicerIndex, v);
        mPagerAdapter.notifyDataSetChanged();
    }
}
    ....

And this works great notifyDataSetChanged() is getting called and the ViewPager's Fragments are getting recreated as I want.

The Problem: When I try to call the notifyDataSetChanged() method of the adapter from another class (In my case it's a class that responsible to parse data that is received from the server) the fragments remain unchanged.

I tried to do this: I created those methods in my FragmentActivity class (the one with the ViewPager):

 public void NotifyTabActivityViewPagerAdapter()
{
    mPagerAdapter.notifyDataSetChanged();
}

public ViewPagerAdapter getTabActivityViewPagerAdapter()
{
    return mPagerAdapter;
}

public ViewPager getTabActivityViewPager()
{
    return mViewPager;
}

And tried the following from the second class:

((TabsViewPagerFragmentActivity)currentActivity).NotifyTabActivityViewPagerAdapter();

Or:

((TabsViewPagerFragmentActivity)currentActivity).getTabActivityViewPager().setAdapter(((TabsViewPagerFragmentActivity)currentActivity).getTabActivityViewPagerAdapter());

But non of this works. Strangely when I run this:

((TabsViewPagerFragmentActivity)currentActivity).getTabActivityViewPager().setAdapter(null);

The fragments do get removed, But I can't recreate the new set of fragments with the new data received from server side.

What am I missing here? How should I recreate all the fragment in this FragmentActivity from another class?

Any assistance would be appreciated.

Community
  • 1
  • 1
Emil Adz
  • 40,709
  • 36
  • 140
  • 187

2 Answers2

2

I don't think this is the case and I think that you should make sure that NotifyTabActivityViewPagerAdapter is running at onPostExecute, but I had a project where notifyDataSetChange didn't work and the fragment remained empty. To solve it, I override the getItemPosition in the adapter as follows:

 @Override
 public int getItemPosition(Object object) {
  return POSITION_NONE;
 }

By returning POSITION_NONE you make sure that the adapter refresh all its data.

Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130
  • You can see from the first question that I posted a link here: http://stackoverflow.com/questions/16671695/how-to-recreate-all-fragments-in-viewpager I already added this method to the adapter. – Emil Adz Jun 02 '13 at 14:10
1

There's really like 7 questions in there. But I understand the main question to be updating or sending data to a FragmentActivity associated with a ViewPager from some other class. I don't know this other classes relationship to the FragmentActivity or ViewPager but there are 2 ways I know of that could handle updating or transfering information the way you want

(1) an interface

If the class you're referring to is another Fragment and loaded in the same ViewPager the way you would update the data (or call a method) in another Fragment or FragmentActivity is with an interface. You can scroll to the bottom of that link for some sample code but basicly you create a bridge between the Fragment and the activity that contains the ViewPager (i.e. the fragment's host activity). You then create a "listener" that looks for some change in the Fragment, upon which it passes some information to the hoast activity. The host activity then calls a public method within the second Fragment you need to update. Only tricky part here is to remember to initialize your interface "listener"within your Fragment's onAttach method or it won't work.

(2) an intent

If you're dealing with two seperate activities just use an Intent. Pass an intent to the FragmentActivity you're looking for and override onNewIntent(Intent i) to perform whatever function you want within the recieving class.

Hope that helps - I'd need more specificy to answer futher because there's a lot of sub-questions in there.

Rarw
  • 7,645
  • 3
  • 28
  • 46
  • Actually this second class is a helper class, and is not extending Fragment or Activity. And there is only one question here: "How should I recreate all the fragment in this FragmentActivity from another class?" the flow goes like this: I have a custom View in my FragmentActivity, the custom view pops a popup, choosing an option from this pop up starts an AsyncTask that gets data from the server, Next I have a helper class (a simple class) that parses this data, and when it finishes I would like to run .notifyDataSetChanged(); from this class or some other way as I did in the listener. – Emil Adz May 28 '13 at 15:54
  • Ok, I didn't understand that from before. If you're class is just implementing `AsyncTack` why can't you just set up a callback between that class and its `onPostExecute` method? – Rarw May 28 '13 at 16:05
  • Because in onPostExecute I call a method "ResponseHandler.ParseRecievedData(String data)", where "ResponseHandler" is a simple java class where I store all my methods that deal with AsyncTask web requests. I want to keep this separation. but I can't undestand how to Call NotifyDataSetChanged() from the "ResponseHandler" class. Does it has to do some thing with the Activity/Application context? – Emil Adz May 30 '13 at 13:06
  • Just add another method in your `onPostExecute` that calls it once your response is parsed. And yes, it has to do with the Context - [see this post](http://stackoverflow.com/questions/6119305/android-how-to-run-asynctask-from-different-class-file) on how to use Context when running AsncTask from a seperate class – Rarw May 30 '13 at 13:11
  • Calling NotifyDataSetChanged does work from the onPostExecute, but for some reason not from my helper class to which I pass my activity. weird. Thanks a lot for your help. – Emil Adz Jun 06 '13 at 12:41
  • That is weird. If you figure out why that is please add it to the post. – Rarw Jun 06 '13 at 12:56
  • I will do that, if I will figure out the source of this problem. – Emil Adz Jun 06 '13 at 13:11