Sending String data between Activities, using them on onClick ()

2

I've been doing the sending of a string data to another activity is called a caculate and from that to history but it has not worked for me can someone tell me what the inconvenience will be. Thanks!

the variables are declared String d; String value, x; Within a function called calculate I am performing this operation. x = value;

    Intent miinten = new Intent(Calculate.this, Hitorial.class);
    miinten.putExtra("x",x);

In the other class history I am receiving the data inside a button to see history as well.

When performing the execution, the following appears in debug when I perform the action of viewing history and pressing the button.

Can someone tell me who is does not receive the parameter because? and how can I solve it? Thanks

    
asked by Julian 21.09.2018 в 18:32
source

2 answers

0

Here is an important detail to comment: the values of Bundle are received in onCreate() , you must get here the values of Bundle , save them in variables and then you can use them, in this case within the click action of the button.

If you try to evaluate it in this way, you are not really receiving a Bundle with data, therefore you will not get the value.

 @Override
       public void onClick(View v) {
         Bundle bundle = getIntent().getExtras();
         if(bundle != null){       
            vista.setText("x"+ bundle.getString("x"));
         }    
        }
    });

This would be the correct way, get the value and store it and then use it when you click on the view:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   //*Obten el bundle y obtén el valor de "x".
   Bundle bundle = getIntent().getExtras();
   if(bundle != null){
       valorX = bundle.getString("x");
   }

  ...
  ...

   //*Ahora aquí puedes usar el valor de "x":

   ver2.setOnClickListener(new View.OnClickListener() {
    @Override
       public void onClick(View v) {
            vista.setText("x"+ valorX);
      }
    });


}

Review:

Send data between activities

    
answered by 22.09.2018 / 02:37
source
-2

It is likely that the value was not received correctly in the activity History.class

I recommend you do the following.

Declare a variable at the class level, for example.

private String Micalculo;

Then assign the value to the variable Micalculo but in the Oncreate ().

public void Oncreate(bundle SavedInstanceState){
  Bundle bundle = getIntent().getExtras();
  if(bundle !=null){
    Micalculo = bundle.getString("x");  
  }
}

And then use the variable Micalculo in your Onclick method.

    
answered by 21.09.2018 в 18:53