简体   繁体   中英

get the value of a textview of the parent activity from dialog

I am trying to create currency app. I have a button on the main activity and when you click the button, it opens a dialog box which is like a currency converter. my problem is that I cannot get the value of a textview of the main activity from the dialog that is opened from main activity.

this is how I open my dialog from the main activity

 Button calc = (Button) findViewById(R.id.btn_converter);
    calc.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent myIntent = new Intent(MainActivity.this, Calculator.class);
            MainActivity.this.startActivity(myIntent);
        }
    });

this is how I am trying to read the value of the textview from the dialog

Textview t1 = (TextView) findViewById(R.id.txt_cad_alis)

but "t1" is always null. what am I doing wrong?

Declare the TextView as a member variable (outside of a method, probably right after class declaration. Then initialize the TextView in onCreate()

public class MyActivity extends Activity
{
    TextView t1;

    public void onCreate(...)
    {
        ...
        t1 = (TextView) findViewById(R.id.txt_cad_alis);
        ...
    }

then use something like String text = ti.getText().toString() to get the text inside your Dialog

You are trying to access Textview t1 which is on the first Activity, from your second activity. This is why it's always null.

You should get the value from your TextView when the button is clicked and put it in hte intent.

Button calc = (Button) findViewById(R.id.btn_converter);
calc.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Textview t1 = (TextView) findViewById(R.id.txt_cad_alis);
        String value = t1.getText().toString();
        Intent myIntent = new Intent(MainActivity.this, Calculator.class);
        intent.putExtra("aKey", value);
        MainActivity.this.startActivity(myIntent);
    }
});

Then in the onCreate() method of your second activity you can retrieve the value like this :

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    Intent i = getIntent();
        if (i.hasExtra("aKey")){
             String value = i.getStringExtra("aKey");   
        }
  }

Declare a class attribute for t1 TextView and get its reference in MainActivity's onCreate(). Then, just before starting the CalculatorActivity, get t1's value and pass it through the Intent. In this case, through "myIntent". Hope it helps.

One important thing to remember here is encapsulation--you most certainly should NOT be directly accessing a view belonging to MainActivity from the dialog. Not only does this violate best practices and the principle of encapsulation, but it will also cause you major headaches down the road if you ever want to reuse your dialog in a different context.

The idea is that any individual view (like your dialog) should be usable in any situation if given the right information, and should not rely on the existence of certain fields in its parent class.

What you should do is get the value of the text field in onClick, and then pass it in with the intent to the dialog.

You can't access a View which belongs to another activity. What you can do instead is to read the value before starting your activity, and send it to your second activity by using intent extras :

Intent myIntent = new Intent(MainActivity.this, Calculator.class);
myIntent.putExtra("value", t1.getText().toString());
MainActivity.this.startActivity(myIntent);

In your Calculator class, it's easy to get the value :

protected void onCreate(Bundle savedInstanceState) {
    ...
    String textViewValue = getIntent().getStringExtra("value");
}

Two possible reasons:

  1. You are not calling setContentView() in your onCreate() , or you are passing the wrong layout id to setContentView() .
  2. txt_cad_alis is not the right id of the textview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM