简体   繁体   中英

How to display values passing from one activity in another activity?

I'm passing 2 values of Number1 and Number2 from FirstActivity to SecondActivity here.In the SecondActivity I display those two numbers and then calculate the sum of it after clicking 'plus' button and display the sum in a text view.

public class FirstActivity extends AppCompatActivity {

    Button OkButton;
    EditText et_number1;
    EditText et_number2;
    String number1;
    String number2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        OkButton = (Button) findViewById(R.id.ok_btn);
        et_number1 = (EditText) findViewById(R.id.ac1pt1);
        et_number2 = (EditText) findViewById(R.id.ac1pt2);
        number1 = et_number1.getText().toString();
        number2 = et_number2.getText().toString();

        OkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(FirstActivity.this,"Ok button clicked",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                intent.putExtra("EXTRAN1",number1);
                intent.putExtra("EXTRAN2",number2);
                startActivity(intent);
            }
        });
    }
}

public class SecondActivity extends AppCompatActivity {

    String no1;
    String no2;

    Button plus;
    Button minus;
    Button multiply;
    Button divide;
    TextView answer;
    TextView tv_number1;
    TextView tv_number2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        plus = (Button) findViewById(R.id.plus);
        minus = (Button) findViewById(R.id.minus);
        multiply = (Button) findViewById(R.id.multiply);
        divide = (Button) findViewById(R.id.division);
        answer = (TextView) findViewById(R.id.actv3);

        Intent i = getIntent();
        no1= i.getStringExtra("EXTRAN1");
        no2= i.getStringExtra("EXTRAN2");

        tv_number1 = (TextView ) findViewById(R.id.ac2tv1);
        tv_number2 = (TextView ) findViewById(R.id.ac2tv2);

        tv_number1.setText(no1);
        tv_number2.setText(no2);

        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addition();
            }
        });
    }

    public void addition(){
        double n1 = Double.parseDouble(tv_number1.getText().toString());
        double n2 = Double.parseDouble(tv_number2.getText().toString());
        double result = n1 +n2;
        String r = Double.toString(result);
        answer.setText(r);
    }
}

Eventhough there's no error showing in the code, when I run the app the two values of number 1 and number 2 are not displaying and the calculation also not working. How am I supposed to correct that?

you have set the Variable out of the button click.
and your Variable will be without value.
you should coppy below code to your button click listener code.

    OkButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(FirstActivity.this,"Ok button clicked",Toast.LENGTH_SHORT).show();
    number1 = et_number1.getText().toString();
    number2 = et_number2.getText().toString();
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        intent.putExtra("EXTRAN1",number1);
        intent.putExtra("EXTRAN2",number2);
        startActivity(intent);
    }
});

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