如何在两个点之间填充阵列从另一个活动数据 [英] how to fill array between two points with data from another activity

查看:130
本文介绍了如何在两个点之间填充阵列从另一个活动数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-------已更新-------------------------------------

在这个类我做了算了一笔账:

  ...
ArrayList的<双> final_cores =新的ArrayList<双>();
...
公共无效cores_func(){
             双initcores = Double.parseDouble(。num_cores.getText()的toString()修剪());
             双half_time = Double.parseDouble(。halftimecores.getText()的toString()修剪());
             双TTIME = Double.parseDouble(timecores.getText()的toString()修剪());
             双L =将Math.log(2)/ half_time;
             双fcores = initcores * Math.exp(-l * TTIME);
             的for(int i = 0; I< = TTIME;我++){
                  final_cores.add(ini​​tcores * Math.exp(-l * TTIME));
             }             //转换ArrayList的加倍[]             双[] = MYDATA新的双[final_cores.size()];
             的for(int i = 0; I< final_cores.size();我++){
                 MYDATA [I] = final_cores.get(ⅰ);
             }
             意图I =新意图(这一点,core_calcs.class);
             i.putExtra(价值,fcores);
             i.putExtra(值2,initcores);
             i.putExtra(值3,TTIME);
             i.putExtra(final_cores,MYDATA);
                     startActivity(ⅰ);
         }
          ...

和在线图类:

  ...
私人双[] = final_cores新的双[100];
市民双[] getFinal_cores(){返回this.mydata;}
公共无效setFinal_cores(双[] final_cores){this.mydata = MYDATA;}
公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        捆绑额外= getIntent()getExtras()。
...
双[] = MYDATA extras.getDoubleArray(final_cores);
setFinal_cores(MYDATA);公共意向getIntent(上下文的背景下){
...
 双[] = MYDATA getFinal_cores();ArrayList的<双> X =新的ArrayList<双>();
        ArrayList的<双> Y =新的ArrayList<双>();
       // ArrayList的<双>数据=新的ArrayList<双>();         //填写的x,y值
         的for(int i = 0; I< = 10;我++){
             x.add(TTIME / 10.0 * I);
         }        的for(int i = 0; I< = 10;我++){
         y.add(ini​​tcores +((MYDATA [I])/ 10.0 * I));     }
        TimeSeries的系列=新的TimeSeries(衰减);
        的for(int i = 0; I< x.size();我++){
            series.add(x.get(ⅰ),y.get(ⅰ));
        }


解决方案

1),不幸的是,你把它似乎并不存在通过双重的ArrayList的简单方法。有两种方法可以解决这个:

a)你需要创建一个围绕双(注意包装类:原始而不是类),并扩展了Parcelable(指南是在这里:的 http://developer.android.com/reference/android/os/Parcelable.html )。如果这个类被称为NewDouble,您可以使用下面的code:

 的ArrayList< NewDouble> final_cores =(ArrayList的&所述; NewDouble&GT)extras.getParcelableArrayList(final_cores);

b)如果您ArrayList中转换为双[]把它放在包之前,你可以使用getDoubleArray()为双阵列。

快速code到一个ArrayList转换为双数组:

 双击[] =的doubleArray新的双[final_cores.size()];
    的for(int i = 0; I< final_cores.size();我++){
        的doubleArray [I] = final_cores.get(ⅰ);
    }

-------UPDATED-------------------------------------

In this class i do the calculations:

...
ArrayList<Double> final_cores =new ArrayList<Double>();
...
public void cores_func(){
             double initcores=Double.parseDouble(num_cores.getText().toString().trim());
             double half_time=Double.parseDouble(halftimecores.getText().toString().trim());
             double ttime=Double.parseDouble(timecores.getText().toString().trim());
             double l=Math.log(2)/half_time;
             double fcores=initcores*Math.exp(-l*ttime);
             for (int i=0;i<=ttime;i++){
                  final_cores.add(initcores*Math.exp(-l*ttime));


             }

             //convert ArrayList to double[]

             double[] mydata = new double[final_cores.size()];
             for(int i = 0; i < final_cores.size(); i++) {
                 mydata[i] = final_cores.get(i);
             }
             Intent i=new Intent(this,core_calcs.class);
             i.putExtra("value",fcores);
             i.putExtra("value2",initcores);
             i.putExtra("value3",ttime);
             i.putExtra("final_cores",mydata);
                     startActivity(i);  
         }
          ...

and in the linegraph class :

...
private double [] final_cores =new double [100];
public double []  getFinal_cores(){ return this.mydata;} 
public void setFinal_cores( double [] final_cores){ this.mydata=mydata;} 
public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        Bundle extras=getIntent().getExtras();
...
double [] mydata = extras.getDoubleArray("final_cores");
setFinal_cores(mydata);

public Intent getIntent(Context context){
...
 double []mydata=getFinal_cores();

ArrayList<Double> x =new ArrayList<Double>();
        ArrayList<Double> y =new ArrayList<Double>();
       // ArrayList<Double> data =new ArrayList<Double>();

         //fill x,y values
         for (int i=0;i<=10;i++){ 
             x.add(ttime / 10.0 * i);
         } 

        for (int i=0;i<=10;i++){ 
         y.add(initcores + ((mydata[i]) / 10.0 * i)); 

     } 


        TimeSeries series = new TimeSeries("Decay");
        for (int i=0;i<x.size();i++){
            series.add(x.get(i),y.get(i));          
        }

解决方案

1) Unfortunately for you it does not seem that there is an easy way to pass double ArrayLists. There are two ways you can approach this:

a) You need to create a class that wraps around a double (note: the primitive not the class) and extends Parcelable (guide is here: http://developer.android.com/reference/android/os/Parcelable.html). If this class is called NewDouble, you can use the code below:

ArrayList<NewDouble> final_cores = (ArrayList<NewDouble>)extras.getParcelableArrayList("final_cores");

b) If you convert the ArrayList to a double[] before putting it in the bundle, you can use getDoubleArray() to a double array.

Quick code to convert an ArrayList to a double array:

    double[] doubleArray = new double[final_cores.size()];
    for(int i = 0; i < final_cores.size(); i++) {
        doubleArray[i] = final_cores.get(i);
    }

这篇关于如何在两个点之间填充阵列从另一个活动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆