how to retrieve a Double value from one activity to another?

匆匆过客 提交于 2019-12-07 12:32:29

问题


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..


回答1:


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)



回答2:


Use Double.parseDouble(my_string);

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




回答3:


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




回答4:


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




回答5:


  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.



来源:https://stackoverflow.com/questions/14643051/how-to-retrieve-a-double-value-from-one-activity-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!