0

I have a program wherein i need to update values realtime in a fragment as and when it is entered in the fragmentactivity.The snapshot is shown below

enter image description here

here as and when i input values in Fragmentactivity (upper half of image)edittext fields of AL,K1 etc the answer must be updated in fragment below . How am i supposed to send values to fragment realtime. help me i am a beginner

human
  • 637
  • 4
  • 15
  • 41

1 Answers1

1

In the class you created the fragment, create a method called public void updateText(String text). In this method, do all calculations, etc. required to calculate the number in the fragment with the String being passed in and then set the text to the String you get after all calculations. In the FragmentActivity, add a textChangedListener by using editText1.addTextChangedListener(this) and adding implements TextWatcher to the class declaration. Add imports and all unimplemented methods and in the recently added onTextChanged method, call the updateText(String) method by saying fragment.updateText(String), where fragment is the Fragment object you created when you attached it to your FragmentActivity.

I hope this helps! Let me know if you need something else.

Alternatively, you can change the updateText(String) method to add or change the parameters to whatever best fits your needs.

Below is a rough example that hopefully will help.

package com.smarticle.example;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class Example extends Activity implements TextWatcher {

ExampleFragment exampleFragment;
EditText editText1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    exampleFragment = new ExampleFragment();
    editText1 = (EditText) findViewById(R.id.editText1);
    editText1.addTextChangedListener(this);
}

@Override
protected void onPause() {
    super.onPause();
    editText1.removeTextChangedListener(this);
}

public void afterTextChanged(Editable s) {
    // I don't implement this, but it is required.
}

public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // I don't implement this, but it is required.
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
    exampleFragment.updateText(editText1.getText().toString().trim());
}

public class ExampleFragment extends Fragment {

    public void updateText(String text) {
        // Perform any necessary calculations or actions
    }

}

}

Adam
  • 2,532
  • 1
  • 24
  • 34
  • can you please provide a sample piece of code. i am confused. – human Feb 12 '13 at 15:40
  • @Coder101 I added an example that shows what I was describing. Please remove your -1. Hopefully my example will help. – Adam Apr 19 '13 at 14:28
  • 1
    Not very good separation of concerns here. The Activity, in this case, has to specifically know about the fragment. It would probably be better to create an interface for them to communicate, and then if you ever change the fragment to a new one, all it has to do is implement the interface and then you know it has all the required functionality to receive values from its parent activity. – paul_f Mar 01 '19 at 10:20
  • @paul_f you are correct, this is a quick-and-dirty solution that has lots of room to improve. You should always use [SOLID](https://en.wikipedia.org/wiki/SOLID) principles. I believe you are specifically pointing out an opportunity to use dependency-inversion. – Adam Mar 14 '19 at 19:30