Android Set Text of TextView in Fragment that is in FragmentPagerAdapter

前端 未结 4 1696

This one is driving me nuts. Basically, I want to create a ViewPager and add a few Fragments to it. Then, all I want to do, it set a value in one of th

4条回答
  •  名媛妹妹
    2021-02-04 15:38

    Unless you are planning to change the value at runtime, you can pass the value into the fragment as a parameter. It is done my using a Bundle and passing it as args into a Fragment, which then retrieves it from it's args. More info here. If you implement this, your instantiation of new Fragments might look something like this:

    public InstallAdapter(FragmentManager fm) {
                super(fm);
                FRAGMENTS = new ArrayList();
                FRAGMENTS.add(FragmentA.newInstance(""));
                FRAGMENTS.add(FragmentB.newInstance(""));
            }
    

    If, however, you are planning to update the value at runtime (it will change as user is running the app), then you want to use an Interface to channell communication between your fragment and your activity. Info here. This is what it might look like:

    //Declare your values for activity;
        ISetTextInFragment setText;
        ISetTextInFragment setText2;
    ...
    //Add interface
    public interface ISetTextInFragment{
        public abstract void showText(String testToShow);
    }
    ...
    //your new InstallAdapter
    public InstallAdapter(FragmentManager fm) {
            super(fm);
    
            FRAGMENTS = new ArrayList();
    
            Fragment fragA = new FragmentA();
            setText= (ISetTextInFragment)fragA;
            FRAGMENTS.add(fragA);
    
            Fragment fragB = new FragmentB();
            setText2= (ISetTextInFragment)fragB;
            FRAGMENTS.add(fragB);
    }
    
    //then, you can do this from your activity:
    ...
    setText.showText("text to show");
    ...
    

    and it will update your text view in the fragment.

    While it can be done "more easily", these methods are recomended because they reduce chances of bugs and make code a lot more readable and maintainable.

    EDIT: this is what your Fragment should look like (modified your code):

    public class FragmentA extends Fragment implements ISetTextInFragment {
    
        TextView myTextView;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle inState) {
            View v = inflater.inflate(R.layout.fragment_a, container, false);
            myTextView = (TextView)v.findViewbyId(R.id.someTextView)
            return v;
        }
    
        @Override
        public void showText(String text) {
            myTextView.setText(text);
        }
    }
    

    If after that you are still getting a null pointer exception, your TextView is NOT located where it needs to me, namely in the R.layout.fragment_a filem, and it needs to be located there. Unless you are calling the interface method BEFORE the fragment finished loading, of course.

提交回复
热议问题