Update textView in Fragment from Activity

后端 未结 2 523
萌比男神i
萌比男神i 2021-01-27 16:25

I have 3 TextView in Fragment..

In MainActivity I have method which generate 3 string every 10 seconds.

And now I want to update TextViews from activity, but I

相关标签:
2条回答
  • 2021-01-27 16:44

    It could be achieved using an interface in your activity, and passing that parameter to your fragment whenever the string is generated:

    public class MainActivity extends AppCompatActivity {
    private OnGenerateStringListener onGenerateStringListener;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        onGenerateStringListener = (OnGenerateStringListener) this;
    
        onStartGenerateStringFragment();
    
        try {
            generateString();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    
    }
    
    private void generateString() throws InterruptedException {
    
        // After string a is generated
        String a = "test one";
        onGenerateStringListener.onGeneratedString(a);
    
        // Sleep 10 secs
        Thread.sleep(10000);
    
        String b = "test Two";
        onGenerateStringListener.onGeneratedString(b);
    
        // Sleep 10 secs
        Thread.sleep(10000);
    
        String c = "test Three";
        onGenerateStringListener.onGeneratedString(c);
    
    }
    
    private void onStartGenerateStringFragment() {
     //Method to launch your fragment
    }    
    
    interface OnGenerateStringListener {
        void onGeneratedString(String string);
    }
    
    }
    

    And this is your Fragment class:

    public class FragmentTextView extends Fragment implements MainActivity.OnGenerateStringListener{
    
        private View view;
        private TextView textViewOne;
        private TextView textViewTwo;
        private TextView textViewThree;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            if (view != null) {
                ViewGroup group = (ViewGroup) view.getParent();
                if (group != null) {
                    group.removeView(view);
                }
            } else {
                view = inflater.inflate(R.layout.fragment_layout, container, false);
                textViewOne = view.findViewById(R.id.textViewOne);
                textViewTwo = view.findViewById(R.id.textViewTwo);
                textViewThree = view.findViewById(R.id.textViewThree);
            }
            return view;
        }
    
    
        @Override
        public void onGeneratedString(String string) {
            switch (string) {
                // Use a switch case to determine which text view gets what parameters
                // for the sake of the example, I just passed a dummy text view input
                case "test one":
                    textViewOne.setText(string);
                    break;
                case "test Two":
                    textViewTwo.setText(string);
                    break;
                case "test Three":
                    textViewThree.setText(string);
                    break;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-27 16:59

    Set id's for the textviews in the xml file: android:id="@+id/myId Then do this:

    TextView textView = (TextView) findViewById(R.id.myId);
    textView.setText("updated text");
    
    0 讨论(0)
提交回复
热议问题