Pass textview value to another screen and convert to double again?

Asked

Viewed 387 times

-2

So I have a value stored in a text view of a previous calculation in Activity 1 and I need that value in Activity 2 to perform other calculations. Pass only the value in String I can pass, what I can’t do is convert to double and do the calculations. There is and when I tried to double as well as be in the images the code is working, but the value that is appearing and the value 2 that sets in default value.

Follow the source codes.

Code of Activity 1:

public void padiola (View v){




    Intent telapad = new Intent(this,Padiola.class);
    telapad.putExtra("chavea",vua.getText().toString());
    telapad.putExtra("chaveb",vub.getText().toString());
    startActivity(telapad);







}

}

Code of Activity 2:

TextView qtda, qtdb, alta, altb, vuar, vubr;


@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_padiola);





    qtda = (TextView) findViewById(R.id.qtdPadareia);
    qtdb = (TextView) findViewById(R.id.qtdPadbrita);
    alta = (TextView) findViewById(R.id.altAreia);
    altb = (TextView) findViewById(R.id.altBrita);
    vuar = (TextView) findViewById(R.id.novoVA);
    vubr = (TextView) findViewById(R.id.novoVB);



   Double va = getIntent().getDoubleExtra("chavea",2f);
   Double vb = getIntent().getDoubleExtra("chaveb",2f);


    vuar.setText(Double.toString(va));
    vubr.setText(Double.toString(vb));




}

public void trintaxquarenta(View v) {

    //Para Areia//

    double s1 = Double.parseDouble(vuar.getText().toString());
    double s2 = Double.parseDouble(vubr.getText().toString());

    double s3 = s1 + s2;

    qtda.setText(Double.toString(s3));

}

}

  • 1

    Hello, all right? Kindly, Would you be able to post the codes instead of images? It would be more useful and practical.

  • with you yes !!!

2 answers

0


You cannot get the value in the second Activity because you are passing a String from Activity A to B and Activity B is trying to get a Double instead of a String. When you arrive at the second Activity, you must first use the getStringExtra() method and then convert the obtained value to Double as desired. Example:

Double va = Double.parseDouble(getIntent().getStringExtra("chavea"));
Double vb = Double.parseDouble(getIntent().getStringExtra("chaveb"));

That should solve your problem.

  • So I tested it that way and it didn’t work. When I click the button to go to Activity B the app closes. I tried to take as String and then convert and also did not work.

-1

Whoa, blah, blah! Since you had doubts about where and when to retrieve the values in your Activity A, I posted the full code.

In his Activity A:

public class MainActivity extends AppCompatActivity {

    //declarando os campos de valores
    EditText valor1, valor2;
    //Botao para a proxima intent
    Button botaoProximaIntent;
    //valores double para a proxima intent
    double a, b;

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

        valor1 = findViewById(R.id.valor1);
        valor2 = findViewById(R.id.valor2);
        botaoProximaIntent = findViewById(R.id.botaoProximaIntent);
        //click no botao
        botaoProximaIntent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //faça as validacoes antes de pegar os valores
                //resgate seu double A do editText
                a = Double.parseDouble(valor1.getText().toString());
                //resgate seu double A do editText
                b = Double.parseDouble(valor2.getText().toString());

                //agora temos o valor de a e b, vamos chamar a proxima intent e passar os parametros
                Intent intent = new Intent(MainActivity.this, SegundaActivity.class);
                //declaramos o bundle
                Bundle params = new Bundle();
                //parametros do bundle
                params.putDouble("valor1", a);
                params.putDouble("valor2", b);
                intent.putExtras(params);
                //chamamos a proxima intent
                startActivity(intent);
            }
        });
    }
}

In your Activity B, receive the values:

public class SegundaActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_segunda);
        //declarando os valores
        double a, b;
        // pegando os parametros intent
        Intent intent = getIntent();
        //validando a intent
        if (intent != null)
        {
            Bundle params = intent.getExtras();
            //validando se há parametros
            if  (params != null)
            {
                //resgatando os valores na segunda intent
                a = params.getDouble("valor1");
                b = params.getDouble("valor2");
            }
        }
    }
}

inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.