Get id of TextView in Fragment from FragmentActivity in ViewPager

后端 未结 2 1726
心在旅途
心在旅途 2020-12-10 15:47

I\'m working with ViewPager with 3 Fragments and I want to change text of TextView in third page.

In that page I have a

2条回答
  •  被撕碎了的回忆
    2020-12-10 16:01

    You cant access text view from fragment activity because its located on fragment and your fragment activity layout don't have any text view with this id. You have to access text view from your third fragment where you used it in its layout. Then access that object from your fragment activity.

    in your fragment do something like this

    TextView mTextView;
    mTextView = (TextView)getView().findViewById(R.id.your_text_view);
    

    Create a function like this

    public void changeText(String mText)
    {
    mTextView.setText(mText);
    }
    

    In your activity result

    //Do not create new object each time you set text. Use the same fragment object which you use for view pager.
            Your_Fragment mFragment;
             protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                    switch (requestCode) {
                        case FILE_SELECT_CODE:
                        if (resultCode == RESULT_OK) {
    
        //Set text from here
                    mFragment.changeText(imgName);
    
                   }
    

提交回复
热议问题