4

I have made an application containing 2 activities ,in that first activity contains some EditTexts (decimal numbers),and anothe ractivity also contains some Edtitexts(decimal) ,now I want to pass one EditText's value to another but as a "double" not as a string.Because that values will be used in mathematical calculation. I want that values in "double" strictly. I have tried as below: but have no idea how to cast a String to "double".

activity1.java

    final Double d1;
        final Double d2;
        final Double d3;
        final Double d4;
        final Double d5;
        final Double d6;
        final Double d7;
        final Double d8;
        d1=Double.parseDouble(et1.getText().toString());
        d2=Double.parseDouble(et2.getText().toString());
        d3=Double.parseDouble(et3.getText().toString());
        d4=Double.parseDouble(et4.getText().toString());
        d5=Double.parseDouble(et5.getText().toString());
        d6=Double.parseDouble(et6.getText().toString());
        d7=Double.parseDouble(et7.getText().toString());
        d8=Double.parseDouble(et8.getText().toString());
.
.
.
.
Intent ic = new Intent(Calculator_1Activity.this,Calculator2a.class);
        ic.putExtra("doubleValue_e1", d1);
        ic.putExtra("doubleValue_e2", d2);
        ic.putExtra("doubleValue_e3", d3);
        ic.putExtra("doubleValue_e4", d4);
        ic.putExtra("doubleValue_e5", d5);
        ic.putExtra("doubleValue_e6", d6);
        ic.putExtra("doubleValue_e7", d7);
        ic.putExtra("doubleValue_e8", d8);

        startActivity(ic);

Activity2.java

Intent receiveIntent = this.getIntent();
        e1 = receiveIntent.getDoubleExtra("doubleValue_e1", 0.00);
        e2 = receiveIntent.getDoubleExtra("doubleValue_e2", 0.00);
        e3 = receiveIntent.getDoubleExtra("doubleValue_e3", 0.00);
        e4 = receiveIntent.getDoubleExtra("doubleValue_e4", 0.00);
        e5 = receiveIntent.getDoubleExtra("doubleValue_e5", 0.00);
        e6 = receiveIntent.getDoubleExtra("doubleValue_e6", 0.00);
        e7 = receiveIntent.getDoubleExtra("doubleValue_e7", 0.00);
        e8 = receiveIntent.getDoubleExtra("doubleValue_e8", 0.00);

        Calculator_1Activity cal1 = new Calculator_1Activity();
        et1.setText(cal1.et1.getText().toString());

i have tried this but still not working.my project stops unexpectly..

jimmy cool
  • 147
  • 3
  • 5
  • 14
  • Possible duplicate of [Passing a double value through to a different class in Android Java](http://stackoverflow.com/questions/9588026/passing-a-double-value-through-to-a-different-class-in-android-java) – Hitesh Sahu Apr 30 '16 at 00:47

5 Answers5

10

Sending Double From First Activity :

Intent sendingIntent = new Intent(this,SecondActivity.class);
intent.putExtra("doubleValue_e1", doubleData);
intent.putExtra("doubleValue_e2", doubleData);
intent.putExtra("doubleValue_e3", doubleData);
intent.putExtra("doubleValue_e4", doubleData);
startActivity(sendingIntent);

Receiving Double in Second Activity :

double e1,e2,e3,e4;
Intent receiveIntent = this.getIntent();
e1 = intent.getDoubleExtra("doubleValue_e1", defaultValue)
e2 = intent.getDoubleExtra("doubleValue_e2", defaultValue)
e3 = intent.getDoubleExtra("doubleValue_e3", defaultValue)
e4 = intent.getDoubleExtra("doubleValue_e4", defaultValue)
Miral Dhokiya
  • 1,720
  • 13
  • 26
0

Use Double.parseDouble(my_string);

See http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#parseDouble%28java.lang.String%29

Dan
  • 1,539
  • 12
  • 23
0

Double.parseDouble(String s) the use a Bundle to pass info from one activity to other.

Jachu
  • 405
  • 5
  • 10
0

Parse your Double to String, put into bundle, and get parse back to string in another activity

Bobans
  • 439
  • 6
  • 13
0
  1. Convert your double DATA to a string using the Double.toString() method.
  2. Send this string over the intent.

    intent.putExtra("DoubleWhichNowIsAString", DATA);
    
  3. Get it back at receiver as

    getIntent().getStringExtra("DoubleWhichNowIsAString")
    
  4. Parse back the string to a double using Double.parseDouble() function.

Alex Myers
  • 6,196
  • 7
  • 23
  • 39
Manthan_Admane
  • 431
  • 6
  • 9