How to pass double type variable through Bundle class?

Asked

Viewed 453 times

0

I cannot pass a textview value from an Activity A to Activity B to perform some calculations. I tried through the putExtra method to pass variable of type string and then convert to double and could not and already tried also by Bundle and also could not, I can only pass variables of type string independent of which method I use, but I can not convert it. And when sure pass the variables the button to perform the calculations does not work and closes the app. What should I do to be able to pass the variable and make the calculation through the button in Activity B?

Follow the string passing code by Bundle.

excerpt from the Activity A code where the button calls Activity B:

public void padiola (View v){




    Intent telapad = new Intent(this,Padiola.class);

    Bundle parametros = new Bundle();

    String va = vua.getText().toString();
    String vb = vub.getText().toString();

    parametros.putString("chavea", va);
    parametros.putString("chaveb", vb);

    telapad.putExtras(parametros);

    startActivity(telapad);


}

Activity B:

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);

    Intent telapad = getIntent();

    Bundle parametros = telapad.getExtras();

    String va1 = parametros.getString("chavea");
    String vb1 = parametros.getString("chaveb");

    vuar.setText(va1);
    vubr.setText(vb1);

}

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));

}

}

  • I don’t see anything wrong with the code. What do you mean by "I can’t"? It makes a mistake?

  • The code described above is string passthrough, the code is right. But I need a variable type double to be able to do the calculations and I am not able to convert the string to double, because when I click on the button to perform the sum it of the error and close the app.

  • Then it is because the text in Edittext is not valid as Double. See if the problem is not exchange point for comma, or vise-versa.

  • But the values are not in Edittext, they are stored in Text View. Like so point or comma ?

  • Put the error that gives.

  • The mistake is that the App has stopped working, just This.

  • 1

    After vse commented on and went to research and identified that the conversion problem was that the string was comma and was not converting to double. I got it through the replace method.

Show 2 more comments

1 answer

0

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

  • I recover the double values outside of the Intent and inside the button execution device ?

  • I tested this way that indicated me and it didn’t work. By clicking on the Activity button the app is closing with error.

  • I tested it before I told you to, because I usually go by putExtra. but I’ll edit it for you to see how it turned out, you must be missing something simple.

  • Reply edited @IF12, see how was the complete code.

  • But the value I need to pass is set in text view and is not an Edit text or and the same thing ?

  • From the same, exchange Edtittext statements for Textview, the rest follows the same thing

  • I switched the declarations and it didn’t work again. Error in the Button that opens Activity B. It closes the application.

  • could put error in logcat? must be declaring something wrong

  • E/Androidruntime: FATAL EXCEPTION: main Process: com.example.irineufarias.appconcrete, PID: 7797 java.lang.Illegalstateexception: Could not execute method for android:onClick Caused by: java.lang.reflect.Invocationtargetexception at java.lang.reflect.Method.invoke(Native Method) Caused by: java.lang.Numberformatexception: For input string: "50,50"

  • Dude, the mistake is there in Exception, you’re putting 50.50. The right double is 50.50

  • Yes, when the value is shown in the text view of Activity A it is with a comma and when the string is passed to Activity B is also with a comma and I can’t convert to a dot as it is in double. Has the replace method will work to convert comma to point ?

  • @Leonardo Figueiredo, asked to answer you: of course yes, just you give a replace in value, exchanging , for . String valueFormated = string.replace(",", ".");

  • I did it. I passed variable type string per Intent class without Bundle and then converted to double and used replace method.

  • So : Activity B : String va = Intent.getStringExtra("key"); and to convert I used Double va1 = Double.parseDouble(va.replace("," "." )); and finally to show the value : vuar.setText(Double.toString(va1));

Show 9 more comments

Browser other questions tagged

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