0

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);    }
wizsab
  • 45
  • 2
  • 8

1 Answers1

1

Seems like you need help passing data from one activity to another.

In your sender activity, use the following code when starting the other activity -

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("NAME", name);
intent.putExtra("VALUE", value);
startActivity(intent);

Here, "NAME" and "VALUE" are String constants used to identify the data you are sending and name and valueare variables of the desired type (see the documentation for the list of putExtra() methods).

Now, in your receiving activity, where you want to get the data. Use methods like getIntent().getStringExtra(String name); to get a String or getIntent().getIntExtra(String name, int defaultValue); to get an int (other available methods are listed here). For example.

String name = getIntent().getStringExtra("NAME");
int value = getIntent().getIntExtra("VALUE",0);
textView.setText(name);

Next time when asking a question, try doing the task yourself, and add in the question, what you have already tried and some relevant code to help us understand your problem.

Yashovardhan99
  • 887
  • 11
  • 26
  • I am sorry for not adding the code the first time like I said I am new at this programming – wizsab Dec 02 '18 at 17:22
  • @wizsab that's fine. Format your code with 4 spaces. If this answered your question, please accept it as the answer – Yashovardhan99 Dec 02 '18 at 17:34
  • I have formatted the code but it is still not responding – wizsab Dec 03 '18 at 03:30
  • @wizsab What is not responding? I see that you have used bundles in your code to pass the data. Did you try it directly with the intent as I have shown in my answer? – Yashovardhan99 Dec 06 '18 at 19:37
  • Also, you are not fetching the fresh values from the EditText when switching activities. Fetch the text using ((EditText) `findViewById(R.id.EDITTEXTIDHERE)).getText().toString();` when passing the intent to fetch the updated values. – Yashovardhan99 Dec 06 '18 at 19:39