I am working on a project in which I have to transfer data like names and values from one activity and it would be displayed on a textview of another activity, but everytime I do this the initial value of these variables get displayed instead of the current value which is being updated on the Java class activity please note that this is on android studio and I am just an upcoming programmer please be lenient.
I have used set and get static methods and intents and bundle but it's still displaying the initial value instead of the current value
here is the code
package com.example.android.bmi;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Main2Activity extends AppCompatActivity {
private String name;
private int year;
private double BMI;
public String Diag;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2);}
public void setName(){
EditText nameF = (EditText)findViewById(R.id.name);
name = nameF.getText().toString();
}
public String getName() { return name;}
public void setYear(){
EditText yearF = (EditText)findViewById(R.id.year);
String value = yearF.getText().toString();
year = Integer.parseInt(value);
}
public void setAge() { age = 2018 - year;}
public void setBMI(){
EditText heightF = (EditText)findViewById(R.id.height);
String value = heightF.getText().toString();
height = Integer.parseInt(value);
EditText weightF = (EditText)findViewById(R.id.weight);
String value1 = weightF.getText().toString();
weight = Integer.parseInt(value);
BMI = (weight*567)/(height*height);
}
public String getDiag(){
Diag = "Name: " + name;
Diag += "\nYear of Birth: " + year;
Diag += "\nAge: " + age;
Diag += "\nBMI" + BMI;
return Diag;
}public void mainN(View view) {
String bread = getDiag();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(Main2Activity.this, Main3Activity.class); a.putExtras(basket);
startActivity(a);
}}
and the receiving Activity is
package com.example.android.bmicalculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Main3Activity extends AppCompatActivity {
String display;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3);
TextView tv = (TextView)findViewById(R.id.info);
Bundle gotBasket = getIntent().getExtras();
display = gotBasket.getString("key");
tv.setText(display); }