简体   繁体   中英

JSONArray won't read double from SeekBar

I am quite new to Android and have a problem reading multiple SeekBar double values to the JSONArray. The code I have works for ints (I have other parts that work that way), but I somehow can't make it work with a double value.

I have been searching for answers, but couldn't quite find what I was looking for and fix it.

The data needs to be read from the MainActivity file to the Conclusion class.

MainActivity.java

public void conclusion(View view){
    Intent intent = new Intent(MainActivity.this, Conclusion.class);

    intent.putExtra("s", (seekBar.getProgress() / 4));
    intent.putExtra("s2", (seekBar2.getProgress() / 4));
    intent.putExtra("s3", (seekBar3.getProgress() / 4));
    intent.putExtra("s4", (seekBar4.getProgress() / 4 ));
    intent.putExtra("s5", (seekBar5.getProgress() / 4 ));
    intent.putExtra("s6", (seekBar6.getProgress() / 4 ));
    intent.putExtra("s7", (seekBar7.getProgress() / 4 ));
    intent.putExtra("s8", (seekBar8.getProgress() / 4 ));
    intent.putExtra("s9", (seekBar9.getProgress() / 4 ));

    startActivity(intent);
}

Conclusion.java

    double seekBar = getIntent().getDoubleExtra("s", 0);
    double seekBar2 = getIntent().getDoubleExtra("s2", 0);
    double seekBar3 = getIntent().getDoubleExtra("s3", 0);
    double seekBar4 = getIntent().getDoubleExtra("s4", 0);
    double seekBar5 = getIntent().getDoubleExtra("s5", 0);
    double seekBar6 = getIntent().getDoubleExtra("s6", 0);
    double seekBar7 = getIntent().getDoubleExtra("s7", 0);
    double seekBar8 = getIntent().getDoubleExtra("s8", 0);
    double seekBar9 = getIntent().getDoubleExtra("s9", 0);

and JSONArray in the Conclusion.java both are in the onCreate() method

    JSONArray jscales= new JSONArray();
    jscales.put(seekBar);
    jscales.put(seekBar2);
    jscales.put(seekBar3);
    jscales.put(seekBar4);
    jscales.put(seekBar5);
    jscales.put(seekBar6);
    jscales.put(seekBar7);
    jscales.put(seekBar8);
    jscales.put(seekBar9);

I have tried moving the calculation to different parts, but to no avail. I know the JSONArray should be able to read a double, so that can't be the problem.

How can I read this double value into the JSONArray?

Cast your result you are passing to the Intent.putExtra() method to a double data type or use Intent.putDouble() instead. Like this:

     intent.putExtra("s", ((double)seekBar.getProgress() /

4)); because you are trying to get a double whilst you put in an int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM