Get id of TextView in Fragment from FragmentActivity in ViewPager

后端 未结 2 1719
心在旅途
心在旅途 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);
    
                   }
    
    0 讨论(0)
  • 2020-12-10 16:02

    You can to inflate a layout in which textView is declared:

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.your_layout, this, false);
    

    and then:

    textViewImg = (TextView) view.findViewById(R.id.textViewUrlImgLectura);
    
    0 讨论(0)
提交回复
热议问题