1

Like in the image below, I have an Activity with a ViewPager (with TabLayout) inside. Now I want to refresh for example the text of an TextView in a ViewPager Fragment. How can I do this?

https://i.stack.imgur.com/GknIX.jpg [Sorry for the image link, but I don't have enough reputations yet.]

I already tried something like this:

//In Activity
View root = getLayoutInflater().inflate(R.layout.fragment_home, null);
TextView textView = (TextView) root.findViewById(R.id.text_view);
textView.setText("test");

But I get a NullPointerException for the textView.

Edit 1: I only need to access the components inside one Fragment, so I am not using an array inside the Adapter. I changed my ViewPagerAdapter to this:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    public HomeFragment fragment;

    ...

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return fragment = new HomeFragment();
            case 1:
                ...
            default:
                return null;
        }
    }

But I still get a NullPointerException here:

final Fragment root = viewPagerAdapter.fragment;
Log.d("testtest", String.valueOf(root == null));

3 Answers3

1

Write this code in your activity where you want to change fragment value. Your fragment id is e.g. my_fragment

Fragment frag = getFragmentManager().findFragmentById(R.id.my_fragment)
((TextView) frag.getView().findViewById(R.id.text_view)).setText(s);
Masum
  • 4,879
  • 2
  • 23
  • 28
0

You can create your own Adapter:

private class YourAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener
{
    Fragment screens[];
    public CheckinHistoryAdapter(FragmentManager fm) {
        super(fm);
        screens = new Fragment[3];
        screens[0] = new CoachFragment();
        screens[1] = new LogingFragment();
        screens[2] = new HistoryFragment();
    }

@Override
public Fragment getItem(int index) {
    if(index <= screens.length)
    {
        return screens[index];
    }
    return null;
}

@Override
public int getCount() {
    return screens.length;
}

}

Use it like this

mViewPager.setAdapter(this.mPagerAdapter);

You can access fragment object with getItem method.

Mohamad Ghafourian
  • 1,052
  • 1
  • 14
  • 26
0

If you only need a reference to one Fragment, then just use the position parameter passed into instantiateItem(), and only assign your Fragment reference for the desired position:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    public HomeFragment fragment;

    //...

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new HomeFragment(); //modified
            case 1:
                ...
            default:
                return null;
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
      if (position == 0) {
        fragment = (HomeFragment) createdFragment;
      }
      return createdFragment;
    }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • I still get a `NullPointerException`: java.lang.NullPointerException: Attempt to read from field 'android.view.View de.ccb.weighttracker.fragment.HomeFragment.rootView on a null object reference` – responseinline Apr 22 '16 at 17:48
  • I may be trying to communicate to the ViewPager Fragments View, when it isn't fully setup. When is the first time to communicate without the possibility that the Fragment is null? – responseinline Apr 22 '16 at 18:24
  • At the moment I am communicating in the `onCreate()` of my Activity. I am 100% sure, that this is my problem. I tested it. – responseinline Apr 22 '16 at 18:24
  • Everything correct there, please trust me and just answer the question – responseinline Apr 22 '16 at 18:41
  • "When is the first time to communicate without the possibility that the Fragment is null?" – responseinline Apr 22 '16 at 18:45
  • @responseinline You are correct, it won't work in `onCreate()`, as the FragmentPagerAdapter isn't fully set up yet. The method in this answer is primarily used for communicating with ViewPager Fragments after onCreate(). Why exactly do you need to refresh a TextView from the onCreate() function of the Activity? You can just do it in `onCreateView()` of the Fragment..... – Daniel Nugent Apr 22 '16 at 18:58